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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值