模板方法模式由两部分组成,第一部分是抽象父类,第二部分是具体的实现子类。通常在抽象父类中封装了子类的算法框架,包括实现一些公共方法以及封装子类中所有方法的执行顺序。子类通过继承这个抽象类,也继承了整个
模板方法模式由两部分组成,第一部分是抽象父类,第二部分是具体的实现子类。通常在抽象父类中封装了子类的算法框架,包括实现一些公共方法以及封装子类中所有方法的执行顺序。子类通过继承这个抽象类,也继承了整个算法结构,并且可以选择重写父类的方法。
咖啡和茶的例子:
泡咖啡的流程
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();