Skip to content

Instantly share code, notes, and snippets.

@DoRightt
Created June 6, 2020 16:55
Show Gist options
  • Save DoRightt/87082f57018e1eced356a28c873bc474 to your computer and use it in GitHub Desktop.
Save DoRightt/87082f57018e1eced356a28c873bc474 to your computer and use it in GitHub Desktop.
abstract class Pizza {
name: string;
dough: Dough;
sauce: Sauce;
veggies: Veggies;
cheese: Cheese;
pepperoni: Pepperoni;
clams: Clams;
toppings: string[] = ["Extra Mozzarella"];
getNmae(): string {
return this.name;
}
abstract prepare(): void;
bake(): void {
console.log("Baking 25 minutes...");
}
cut(): void {
console.log("Cutting into slices...");
}
box(): void {
console.log("Place pizza in the box.");
}
setName(name: string): void {
this.name = name;
}
getName(): string {
return this.name;
}
}
class CheesePizza extends Pizza {
ingredientFactory: PizzaIngredientsFactory;
constructor(ingredientFactory: PizzaIngredientsFactory) {
super();
this.ingredientFactory = ingredientFactory;
}
prepare(): void {
console.log(`Preparing ${this.name}`);
this.dough = this.ingredientFactory.createDough();
this.cheese = this.ingredientFactory.createCheese();
this.sauce = this.ingredientFactory.createSauce();
}
}
class PepperoniPizza extends Pizza {
ingredientFactory: PizzaIngredientsFactory;
constructor(ingredientFactory: PizzaIngredientsFactory) {
super();
this.ingredientFactory = ingredientFactory;
}
prepare(): void {
console.log(`Preparing ${this.name}`);
this.dough = this.ingredientFactory.createDough();
this.pepperoni = this.ingredientFactory.createPepperoni();
this.sauce = this.ingredientFactory.createSauce();
this.cheese = this.ingredientFactory.createCheese();
}
}
class ClamPizza extends Pizza {
ingredientFactory: PizzaIngredientsFactory;
constructor(ingredientFactory: PizzaIngredientsFactory) {
super();
this.ingredientFactory = ingredientFactory;
}
prepare(): void {
console.log(`Preparing ${this.name}`);
this.dough = this.ingredientFactory.createDough();
this.cheese = this.ingredientFactory.createCheese();
this.clams = this.ingredientFactory.createClam();
this.sauce = this.ingredientFactory.createSauce();
}
}
class VeggiePizza extends Pizza {
ingredientFactory: PizzaIngredientsFactory;
constructor(ingredientFactory: PizzaIngredientsFactory) {
super();
this.ingredientFactory = ingredientFactory;
}
prepare(): void {
console.log(`Preparing ${this.name}`);
this.dough = this.ingredientFactory.createDough();
this.cheese = this.ingredientFactory.createCheese();
this.veggies = this.ingredientFactory.chreateVeggies();
this.sauce = this.ingredientFactory.createSauce();
}
}
interface PizzaIngredientsFactory {
createDough(): Dough;
createSauce(): Sauce;
createCheese(): Cheese;
chreateVeggies(): Veggies;
createPepperoni(): Pepperoni;
createClam(): Clams;
}
abstract class Dough {}
class ThinCrustDough extends Dough {}
class ThickCrustDough extends Dough {}
abstract class Sauce {}
class MarinaraSauce extends Sauce {}
class PlumTomatoSauce extends Sauce {}
abstract class Cheese {}
class ReggianoCheese extends Cheese {}
class Mozzarella extends Cheese {}
abstract class Pepperoni {}
class SlicedPepperoni extends Pepperoni {}
abstract class Veggies {}
class Spinach extends Veggies {}
class EggPlant extends Veggies {}
class Onion extends Veggies {}
class Garlic extends Veggies {}
class RedPepper extends Veggies {}
class Mushroom extends Veggies {}
class BlackOlives extends Veggies {}
abstract class Clams {}
class FrozenClams extends Clams {}
class FreshClams extends Clams {}
class NYPizzaIngredientsFactory implements PizzaIngredientsFactory {
createDough(): Dough {
return new ThinCrustDough();
}
createSauce(): Sauce {
return new MarinaraSauce();
}
createCheese(): Cheese {
return new ReggianoCheese();
}
chreateVeggies(): Veggies {
return [new Garlic(), new Onion(), new Mushroom(), new RedPepper()];
}
createPepperoni(): Pepperoni {
return new SlicedPepperoni();
}
createClam(): Clams {
return new FreshClams();
}
}
class ChicagoPizzaIngredientsFactory implements PizzaIngredientsFactory {
createDough(): Dough {
return new ThickCrustDough();
}
createSauce(): Sauce {
return new PlumTomatoSauce();
}
createCheese(): Cheese {
return new Mozzarella();
}
chreateVeggies(): Veggies {
return [new Spinach(), new BlackOlives(), new EggPlant()];
}
createPepperoni(): Pepperoni {
return new SlicedPepperoni();
}
createClam(): Clams {
return new FrozenClams();
}
}
abstract class PizzaStore {
orderPizza(type: string): Pizza {
const pizza: Pizza = this.createPizza(type);
console.log(type, pizza);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract createPizza(type: string): Pizza;
}
class NYPizzaStore extends PizzaStore {
protected createPizza(item: string): Pizza {
let pizza: Pizza;
const ingredientFactory = new NYPizzaIngredientsFactory();
if (item === "cheese") {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("NY Style Cheese Pizza");
} else if (item === "clam") {
pizza = new ClamPizza(ingredientFactory);
pizza.setName("NY Style Clam Pizza");
} else if (item === "veggie") {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("NY Style Veggie Pizza");
} else if (item === "pepperoni") {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("NY Style Pepperoni Pizza");
}
return pizza;
}
}
class ChicagoPizzaStore extends PizzaStore {
protected createPizza(item: string): Pizza {
let pizza: Pizza;
const ingredientFactory = new ChicagoPizzaIngredientsFactory();
if (item === "cheese") {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("Chicago Style Cheese Pizza");
} else if (item === "clam") {
pizza = new ClamPizza(ingredientFactory);
pizza.setName("Chicago Style Clam Pizza");
} else if (item === "veggie") {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("Chicago Style Veggie Pizza");
} else if (item === "pepperoni") {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("Chicago Style Pepperoni Pizza");
}
return pizza;
}
}
const chicagoStore = new ChicagoPizzaStore();
const NYStore = new NYPizzaStore();
chicagoStore.orderPizza("cheese");
NYStore.orderPizza("veggie");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment