Skip to content

Instantly share code, notes, and snippets.

abstract class Pie {
protected addFlour(): void {
console.log("Pie: addFlour");
}
protected addEggs(): void {
console.log("Pie: addEggs");
}
protected addWater(): void {
interface Strategy {
execute(): void;
}
class ExploreTheWorldStrategy implements Strategy {
public execute(): void {
console.log("ExploreTheWorldStrategy executed!");
}
}
const coffeeMachine: CoffeeMachine = new CoffeeMachine(new CoffeeMachineReadyState());
coffeeMachine.request();
coffeeMachine.request();
coffeeMachine.request();
coffeeMachine.request();
coffeeMachine.request();
coffeeMachine.request();
coffeeMachine.fillWaterTank(50);
coffeeMachine.request();
coffeeMachine.request();
interface State {
handle(machine: CoffeeMachine): void;
}
class CoffeeMachineReadyState implements State {
public handle(machine: CoffeeMachine): void {
console.log("CoffeeMachineReadyState: ready to work!");
if (machine.isWaterTankEmpty()) {
machine.State = new WaterTankEmptyState();
return;
class ClipboardState {
private copiedContent: string;
constructor(command: string) {
this.copiedContent = command;
}
get Command(): string {
return this.copiedContent;
}
interface Mediator {
notify(sender: object, message: string): void;
}
class Car {
protected mediator: Mediator;
public setMediator(mediator: Mediator): void {
this.mediator = mediator;
}
const cakes = [
"New York Cheesecake",
"Molten Chocolate Cake",
"Tres Leches Cake",
"Schwarzwälder Kirschtorte",
"Cremeschnitte",
"Sachertorte",
"Kasutera",
];
const cakeRecipes: CakeRecipes = new CakeRecipes(cakes);
interface RecipeIterator {
next(): any;
hasNext(): boolean;
}
interface RecipeAggregator {
createIterator(): RecipeIterator;
}
class CakeRecipeIterator implements RecipeIterator {
const videoGame: VideoGame = new VideoGame();
const openInventoryKeyboardCommand: Command = new OpenInventoryKeyboardCommand(videoGame);
const openInventoryButtonCommand: Command = new OpenInventoryButtonCommand(videoGame);
const operatingSystem: OperatingSystem = new OperatingSystem();
operatingSystem.storeAndExecute(openInventoryKeyboardCommand);
operatingSystem.storeAndExecute(openInventoryButtonCommand);
/*
"KeyboardShortcutCommand: execute method called!"
"VideoGame: action is being executed!"
interface Command {
execute(): void;
}
class OpenInventoryKeyboardCommand implements Command {
private videoGame: VideoGame;
constructor(videoGame: VideoGame) {
this.videoGame = videoGame;
}