Last active
January 24, 2020 13:11
-
-
Save iagobelo/33624a7eef11d7743606cfcfbcbcaa8b to your computer and use it in GitHub Desktop.
Factory Method
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
interface Pizza { | |
flavor: string; | |
pieces: number; | |
cut: () => PizzaSlice | null; | |
} | |
interface PizzaSlice { } | |
class MuzzarellaPizzaSlice implements PizzaSlice { } | |
class MuzzarellaPizza implements Pizza { | |
flavor: string = 'Muzzarella'; | |
pieces: number = 8; | |
cut(): PizzaSlice | null { | |
if (this.pieces >= 1) { | |
this.pieces -= 1; | |
return new MuzzarellaPizzaSlice(); | |
} | |
return null; | |
}; | |
} | |
abstract class Pizzeria { | |
public eatPizza() { | |
const pizza: Pizza = this.makePizza(); | |
pizza.cut(); | |
} | |
protected abstract makePizza(): Pizza; | |
} | |
class PizzaHut extends Pizzeria { | |
protected makePizza(): Pizza { | |
return new MuzzarellaPizza(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment