Created
February 25, 2023 14:11
-
-
Save devoNOTbevo/1637312f537ac998d57d3f344da09ebd to your computer and use it in GitHub Desktop.
Object Oriented Tacos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top Level Classes | |
class Person {} | |
class Devon extends Person { | |
public hungry = true; | |
eat(food: Food | Food[]) { | |
// nom nom nom | |
} | |
apply(food: Food, sauce: Sauce) { | |
// put the sauce on the food | |
} | |
} | |
class Food { | |
public edible = true; | |
} | |
// https://cuberule.com/ | |
// representing the "cube" faces as an array of booleans. | |
// [bottom, top, left, right, front, back] | |
class Taco extends Food { | |
starchLocations = [true, false, true, true, false, false]; | |
} | |
class BreakfastTaco extends Taco { | |
public fillings: BreakfastFillings | BreakfastFillings[]; | |
public salsas: Salsa | Salsa[] | null; | |
constructor({ fillings, salsas }: BreakfastIngredients) { | |
super(); | |
this.fillings = fillings; | |
if (salsas) { | |
this.salsas = salsas; | |
} | |
} | |
set salsa(salsa: Salsa) { | |
this.salsa = salsa; | |
} | |
} | |
// types | |
type BreakfastFillings = | |
| 'eggs' | |
| 'bacon' | |
| 'potatoes' | |
| 'beans' | |
| 'peppers' | |
| 'onions'; | |
type Salsa = 'verde' | 'diablo' | 'roja' | 'serrano' | 'avocado' | 'tomatillo'; | |
type Sauce = Salsa; // can extend this union with other sauces | |
type BreakfastIngredients = { | |
salsas?: Salsa | Salsa[] | null; | |
fillings: BreakfastFillings | BreakfastFillings[]; | |
}; | |
// Entry Point aka Chowing Down | |
const consumer = new Devon(); | |
const baconAndEggTaco = new BreakfastTaco({ | |
fillings: ['bacon', 'eggs', 'peppers'], | |
}); | |
const beanAndPotatoTaco = new BreakfastTaco({ | |
fillings: ['beans', 'potatoes'], | |
salsas: ['avocado', 'serrano'] | |
}); | |
consumer.eat(beanAndPotatoTaco) | |
consumer.apply(baconAndEggTaco, 'diablo') | |
consumer.eat(baconAndEggTaco); | |
// DELICIOUS!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A friend of mine (who has familiarity with programming) joked about taking my order for breakfast tacos like this: "when I instantiate tacos, what properties do you want them to have?".
Obviously - being the nerd I am - I wrote a program in response.