typescript设计模式使用方法
程序员文章站
2023-12-30 11:17:40
typescript 设计模式--模板方法
什么时候用到这种模式
1.有稳定的整体操作结构,各个步骤却有改变的需求
2.灵活的实现各个步骤的变换(步骤总是有实现的先后关系)
例子:
ab...
typescript 设计模式--模板方法
什么时候用到这种模式
1.有稳定的整体操作结构,各个步骤却有改变的需求
2.灵活的实现各个步骤的变换(步骤总是有实现的先后关系)
例子:
abstract class cook { public docook() { this.buyrawmaterial(); this.clearmaterial(); this.cooking(); this.outpot(); this.clearpot(); } protected abstract buyrawmaterial(): void; protected abstract clearmaterial(); protected abstract cooking(): void; protected abstract outpot(): void; protected clearpot(): void{ console.log('锅是一定要刷的'); } } class huiguorou extends cook { protected buyrawmaterial() { console.log('买肉'); } protected clearmaterial() { console.log('肉和辣椒洗干净'); } protected cooking() { console.log('很复杂'); } protected outpot() { console.log('好了要出锅的'); } } class tangchupaigu extends cook { protected buyrawmaterial() { console.log('买排骨'); } protected clearmaterial() { console.log('排骨洗干净了'); } protected cooking() { console.log('很复杂,鬼知道怎么做的'); } protected outpot() { console.log('好了要出锅的'); } } class client { test() { console.log('今天吃啥'); let food: cook = new huiguorou(); food.docook(); let food2: cook = new tangchupaigu(); food2.docook(); } } new client().test();