Skip to content

Instantly share code, notes, and snippets.

@iagobelo
Last active January 24, 2020 13:11
Show Gist options
  • Save iagobelo/33624a7eef11d7743606cfcfbcbcaa8b to your computer and use it in GitHub Desktop.
Save iagobelo/33624a7eef11d7743606cfcfbcbcaa8b to your computer and use it in GitHub Desktop.
Factory Method
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