From c7af0ab5b025b9db3bfae489deacc2e17a49f361 Mon Sep 17 00:00:00 2001 From: caojunjie <1301239018@qq.com> Date: Mon, 21 Oct 2024 17:40:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(typescript):=2018-=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\346\263\225\346\250\241\345\274\217.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git "a/DesignPattern/18-\346\250\241\346\235\277\346\226\271\346\263\225\346\250\241\345\274\217.md" "b/DesignPattern/18-\346\250\241\346\235\277\346\226\271\346\263\225\346\250\241\345\274\217.md" index ca90536..474daf2 100644 --- "a/DesignPattern/18-\346\250\241\346\235\277\346\226\271\346\263\225\346\250\241\345\274\217.md" +++ "b/DesignPattern/18-\346\250\241\346\235\277\346\226\271\346\263\225\346\250\241\345\274\217.md" @@ -618,3 +618,89 @@ func main() { } ``` +### Typescript + +``` typescript +abstract class AbstractTemplate { + coffeeName: string; + + constructor(coffee: string) { + this.coffeeName = coffee; + } + templateMethod() { + console.log(`Making ${this.coffeeName}:`); + this.grindingCoffeeBeans(); + this.brewingCoffee(); + this.addingCondiments(); + console.log(); + } + + abstract grindingCoffeeBeans(): void; + abstract brewingCoffee(): void; + abstract addingCondiments(): void; +} + +class AmericaCoffee extends AbstractTemplate { + constructor() { + super("American Coffee"); + } + + grindingCoffeeBeans(): void { + console.log("Grinding coffee beans"); + } + + brewingCoffee(): void { + console.log("Brewing coffee"); + } + + addingCondiments(): void { + console.log("Adding condiments"); + } +} + +class Latte extends AbstractTemplate { + constructor() { + super("Latte"); + } + + grindingCoffeeBeans(): void { + console.log("Grinding coffee beans"); + } + + brewingCoffee(): void { + console.log("Brewing coffee"); + } + + addingCondiments(): void { + console.log("Adding milk"); + console.log("Adding condiments"); + } +} + +// @ts-ignore +entry(2, (...args) => { + args.forEach((type) => { + let coffee: AbstractTemplate; + if (type === 1) { + coffee = new AmericaCoffee(); + } else if (type === 2) { + coffee = new Latte(); + } else { + throw "type error"; + } + + coffee.templateMethod(); + }); +})(1)(2); + +export function entry(count: number, fn: (...args: any) => void) { + function dfs(...args) { + if (args.length < count) { + return (arg) => dfs(...args, arg); + } + + return fn(...args); + } + return dfs; +} +``` \ No newline at end of file