侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

javascript的模版方法模式(五)

2024-05-12 星期日 / 0 评论 / 0 点赞 / 92 阅读 / 4592 字

模板方法模式由两部分组成,第一部分是抽象父类,第二部分是具体的实现子类。通常在抽象父类中封装了子类的算法框架,包括实现一些公共方法以及封装子类中所有方法的执行顺序。子类通过继承这个抽象类,也继承了整个

模板方法模式由两部分组成,第一部分是抽象父类,第二部分是具体的实现子类。通常在抽象父类中封装了子类的算法框架,包括实现一些公共方法以及封装子类中所有方法的执行顺序。子类通过继承这个抽象类,也继承了整个算法结构,并且可以选择重写父类的方法。

咖啡和茶的例子:

泡咖啡的流程

1.       把水煮沸

2.       用沸水冲泡咖啡

3.       把咖啡倒进杯子

4.       加糖和牛奶

//咖啡var Coffee = function(){};  Coffee.prototype.boilWater = function(){    console.log(‘把水煮沸’);  }     Coffee.prototype.brewCoffeeGriends =function(){   console.log(‘用沸水冲泡咖啡’);  }     Coffee.prototype.pourInCup = function(){   console.log(‘把咖啡倒进杯子’);  }     Coffee.prototype.addSugarAndMilk =function(){    console.log(‘加糖加牛奶’);  }     Coffee.prototype.init = function(){    this.boilWater();    this. brewCoffeeGriends();    this. pourInCup();    this. addSugarAndMilk();  }     var coffee = new Coffee();  coffee.init();  

泡茶的流程:

1.       把水煮沸

2.       用沸水浸泡茶叶

3.       把茶水倒进杯子

4.       加柠檬

var Tea = function(){};   Tea.prototype.boilWater = function(){     console.log('把水煮沸');    }   Tea.prototype.steepTeaBag = function(){     console.log('用沸水浸泡茶叶')    }   Tea.prototype.pourInCup = function(){     console.log('把茶水倒进杯子')    }      Tea.prototype.addLemon = function(){     console.log('加柠檬');    }      Tea.prototype.init = function(){       this.boilWater();       this.steepTeaBag();       this.pourInCup();       this.addLemon();   var tea = new Tea();   tea.init(); 

用模版模式:

var Beverage =function(){};Beverage.prototype.boilWater = function(){  		console.log('把水者沸');  };  Beverage.prototype.brew = function(){}; //空方法,应该由子类重写  Beverage.prototype.pourInCup = function(){};  Beverage.prototype.addCondiments = function(){};  Beverage.prototype.init = function(){     this.boilWater();     this.brew();     this.pourInCup();     this.addCondiments();  }var Coffee = function(){}Coffee.prototype = new Beverage();Coffee.prototype.brew = function(){	console.log('用沸水冲泡咖啡');  };Coffee.prototype.pourInCup = function(){	console.log('把咖啡倒进杯子');  };  Coffee.prototype.addCondiments = function(){	console.log('用沸水冲泡咖啡');  };var coffee = new Coffee();  coffee.init();var Tea = function(){}Tea.prototype = new Beverage();Tea.prototype.brew = function(){	console.log('用沸水冲泡茶叶');  };Tea.prototype.pourInCup = function(){	console.log('把茶倒进杯子');  };  Tea.prototype.addCondiments = function(){	console.log('加柠檬');  };var tea = new Tea();  tea.init();

另一种方法:

//模版模式var Beverage =function(param){	var boilWater = function(){		console.log('把水者沸');  	};	var brew = param.brew || function(){		throw new Error('必须传递brew方法'); 	};	var pourInCup = param.pourInCup || function(){		throw new Error('必须传递pourInCup方法');  	};	var addCondiments = param.addCondiments || function(){		throw new Error('必须传递addCondiments方法'); 	};	var F = function(){};	F.prototype.init = function(){		boilWater();		pourInCup();		addCondiments();		}	return F;}var Coffee = Beverage({	brew: function () {        console.log('用沸水冲泡咖啡')      },      pourInCup: function () {        console.log('把咖啡倒塌杯子')      },      addCondiments: function () {          console.log("加糖加奶");      } });var coffee = new Coffee();  coffee.init();var Tea = Beverage({	brew: function () {          console.log('用沸水冲泡茶叶')      },      pourInCup: function () {          console.log('把茶倒塌杯子')      },      addCondiments: function () {          console.log("加柠檬");      }});var tea = new Tea();  tea.init();

 

广告 广告

评论区