当前位置: 移动技术网 > IT编程>网页制作>CSS > typescript设计模式使用方法

typescript设计模式使用方法

2018年10月03日  | 移动技术网IT编程  | 我要评论

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();

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网