I am designing a Menu system for a restaurent in Typescript. I have defined an interface to implement necessary function for each Menu Item :
interface HasPrice {
getPrice(): number;
}
Each menu item is defined by a class.
class Gobi implements HasPrice {
getPrice() : number {
return 50;
}
}
class FriedRice implements HasPrice {
getPrice() : number {
return 100;
}
}
I am using decorator class to add toppings. eg.Fried Rice with chicken
class FriedRiceWithChicken implements HasPrice {
getPrice() : number {
return super.getPrice() + 25;
}
}
Now I want to extend this system for combo menus. eg. Fried Rice + Gobi
class FriedRiceAndGobi extends FriedRice, Gobi {
getPrice() : number {
return FriedRice.getPrice() + Gobi.getPrice();
}
}
How can I implement combo classes? Is there a way I can write a generic class/method to implement combo menu?
Proxyor the like that behaves sort of like multiple inheritance, but you'll never get (const frg = new FriedRiceAndGobi(); console.log(frg instanceof FriedRice && frg instanceof Gobi)to returntrue; at most one of those classes will be in the prototype chain for any object. Composition is likely the way to go here; do you want an answer that shows how to do this?