简介 目录 评价 推荐
  • RelaxLearning 2020-07-22

    高大上的设计模式--什么是模板方法模式

    截图
    0赞 · 0采集
  • 林念志祥 2018-11-12

    设计模式--分离共同点

    1. 咖啡和茶不同,抽象出‘饮料’

    2. 把水煮沸

    3. 泡的方式不同,抽象成‘泡’

    4. 加的调料不同,抽想成‘调料’

    0赞 · 0采集
  • 林念志祥 2018-11-12

    Pattern.js

    var Coffee=function(){};

    Coffee.prototype.boilWater=function(){
    console.log("把水煮沸")
    };
    Coffee.prototype.brewCoffee=function(){
    console.log("用沸水冲泡咖啡")
    };
    Coffee.prototype.pourInCup=function(){
    console.log("把咖啡倒进杯子")
    };
    Coffee.prototype.addSugarAndMilk=function(){
    console.log("加糖和牛奶")
    };
    Coffee.prototype.init=function(){
    this.boilWater()
    this.brewCoffee()
    this.pourInCup()
    this.addSugarAndMilk()
    };
    var Tea=function(){};
    Tea.prototype.boilWater=function(){
    console.log("把水煮沸")
    };
    Tea.prototype.steepTea=function(){
    console.log("用沸水浸泡茶叶")
    };
    Tea.prototype.pourInCup=function(){
    console.log("把茶水倒进杯子")
    };
    Tea.prototype.addLemon=function(){
    console.log("加柠檬")
    };
    Tea.prototype.init=function(){
    this.boilWater();
    this.steepTea();
    this.pourInCup();
    this.addLemon();
    };
    var coffee=new Coffee();
    coffee.init();
    var tea=new Tea();
    tea.init();


    0赞 · 0采集
  • 贤惠 2018-05-09
    模版模式
    截图
    0赞 · 0采集
  • 慕标6569998 2018-04-30

    ctrl+D统一修改

    0赞 · 0采集
  • 彈指 2018-04-18

    var Beverage=function(){};

    Beverage.prototype.boilWater = function() {

    console.log("煮水");

    };

    Beverage.prototype.brew = function() {

        throw new Error("子类必须重写该方法");

    };

    Beverage.prototype.pourInCup = function() {

        throw new Error("子类必须重写该方法");

    };

    Beverage.prototype.addSomething = function() {

        throw new Error("子类必须重写该方法");

    };


    Beverage.prototype.ifWantSometihng=function(){

    retuen true;

    }

    //init是模板方法

    Beverage.prototype.init = function() {

    this.boilWater();

    this.brew();

    this.pourInCup();

    if(this.ifWantSometihng()){

    this.addSomething();

    }

    };



    //--------子类1

    var Coffee = function() {};


    Coffee.prototype.brew = function() {

    console.log("冲咖啡");

    };

    Coffee.prototype.pourInCup = function() {

    console.log("装杯");

    };

    Coffee.prototype.addSomething = function() {

    console.log("加糖");

    };


    //重写ifWantSometihng方法

    Coffee.prototype.ifWantSometihng=function(){

    return window.confirm("加不加?");

    };


    //--------子类2

    var Tea = function() {};

    Tea.prototype.brew = function() {

    console.log("泡茶");

    };

    Tea.prototype.pourInCup = function() {

    console.log("装杯");

    };

    Tea.prototype.addSomething = function() {

    console.log("加柠檬");

    };


    //继承父类

    Coffee.prototype= new Beverage();

    Tea.prototype= new Beverage();


    var coffee = new Coffee(); 

    coffee.init();

    var tea = new Tea(); 

    tea.init();


    0赞 · 0采集
  • weibo_苏打小Q_0 2017-12-06
    模板方法。 只需要继承实现。 抽象父类,具体实现的子类。 prototype 原型。console控制台。 分离出共同点。分离出来抽象父类。统一修改按住控制+D. 通过原型链访问的。强制子类要重写。 可以在父类方法中抛出错误。如果没有重写,就会是父类的错误。 钩子方法。
    0赞 · 2采集
  • SUYI_CHAN 2017-10-29
    模板方法。 只需要继承实现。 抽象父类,具体实现的子类。 prototype 原型。console控制台。 分离出共同点。分离出来抽象父类。统一修改按住控制+D. 通过原型链访问的。强制子类要重写。 可以在父类方法中抛出错误。如果没有重写,就会是父类的错误。 钩子方法。
    0赞 · 0采集
  • sout_main 2017-08-26
    将方法写在构造函数的原型上用prototype 再添加一个调用它的方法init,同样用prototype给添加上去 Coffee.prototype.init = function(){ this.boilWater(); this.addWater(); ... }; 最后调用它: var coffee = new Coffee(); coffee.init();
    截图
    0赞 · 0采集
  • qq_珂壳可克_03751673 2017-08-14
    将方法写在构造函数的原型上用prototype 再添加一个调用它的方法init,同样用prototype给添加上去 Coffee.prototype.init = function(){ this.boilWater(); this.addWater(); ... }; 最后调用它: var coffee = new Coffee(); coffee.init();
    0赞 · 2采集
  • 青青子衿_666 2017-07-04
    模版方法模式 将公共的方法和执行的顺序封装在父类之中
    截图
    0赞 · 1采集
  • 青青子衿_666 2017-07-04
    模版模式
    截图
    0赞 · 1采集
  • 青青子衿_666 2017-07-04
    我还在设计模式的第一阶段
    截图
    0赞 · 1采集
  • 小苹果9 2017-06-14
    看快捷键
    截图
    0赞 · 1采集
数据加载中...
开始学习 免费