实现策略模式和模板方法模式示例,添加支付策略和烹饪流程,增强代码可读性
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
实例:
|
||||
汤宝宝炒西红柿鸡蛋。
|
||||
*/
|
||||
|
||||
abstract class Dish {
|
||||
abstract prepareIngredients(): string;
|
||||
abstract cook(): string;
|
||||
abstract serve(): string;
|
||||
doCooking(): void {
|
||||
console.log("第一步:", this.prepareIngredients());
|
||||
console.log("第二步:", this.cook());
|
||||
console.log("第三步:", this.serve());
|
||||
}
|
||||
}
|
||||
|
||||
class TomatoEggs extends Dish {
|
||||
prepareIngredients(): string {
|
||||
return "准备西红柿和鸡蛋;";
|
||||
}
|
||||
cook(): string {
|
||||
return "炒西红柿和鸡蛋;";
|
||||
}
|
||||
serve(): string {
|
||||
return "装盘,西红柿鸡蛋炒好了。";
|
||||
}
|
||||
}
|
||||
|
||||
(function(){
|
||||
console.log("========== 模板方法模式 ==========");
|
||||
const dish = new TomatoEggs();
|
||||
dish.doCooking();
|
||||
console.log("\n======== 模板方法模式结束 ========");
|
||||
})()
|
||||
Reference in New Issue
Block a user