模板方法模式,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。模板方法模式把不变行为搬到超类中,从而去除了子类中的重复代码。

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

模板方法类图:

模板方法模式中涉及了两个角色:

  • 抽象模板角色(Vegetable扮演这个角色):定义了一个或多个抽象操作,以便让子类实现,这些抽象操作称为基本操作。
  • 具体模板角色(ChineseCabbage和Spinach扮演这个角色):实现父类所定义的一个或多个抽象方法。

C#模板方法:

namespace 模板方法
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("学生甲抄的试卷:");
TestPaper studentA = new TestPaperA();
studentA.TestQuestion1();
studentA.TestQuestion2();
studentA.TestQuestion3(); Console.WriteLine("学生乙抄的试卷:");
TestPaper studentB = new TestPaperB();
studentB.TestQuestion1();
studentB.TestQuestion2();
studentB.TestQuestion3(); Console.Read(); }
} class TestPaper
{
public void TestQuestion1()
{
Console.WriteLine(" 杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[ ] a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 ");
Console.WriteLine("答案:" + Answer1());
} public void TestQuestion2()
{
Console.WriteLine(" 杨过、程英、陆无双铲除了情花,造成[ ] a.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化 ");
Console.WriteLine("答案:" + Answer2());
} public void TestQuestion3()
{
Console.WriteLine(" 蓝凤凰的致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[ ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对 ");
Console.WriteLine("答案:" + Answer3());
} protected virtual string Answer1()
{
return "";
} protected virtual string Answer2()
{
return "";
} protected virtual string Answer3()
{
return "";
} }
//学生甲抄的试卷
class TestPaperA : TestPaper
{
protected override string Answer1()
{
return "b";
} protected override string Answer2()
{
return "c";
} protected override string Answer3()
{
return "a";
}
}
//学生乙抄的试卷
class TestPaperB : TestPaper
{
protected override string Answer1()
{
return "c";
} protected override string Answer2()
{
return "a";
} protected override string Answer3()
{
return "a";
} }
}

js模拟高级语言的模板方法:

var TestPaper = function(){};
TestPaper.prototype.TestQuestion1 = function(){
console.log("杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[ ] a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维");
console.log("答案:" + this.answer1());
};
TestPaper.prototype.TestQuestion2 = function(){
console.log("杨过、程英、陆无双铲除了情花,造成[ ] a.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化");
console.log("答案:" + this.answer2());
};
TestPaper.prototype.TestQuestion3 = function(){
console.log("蓝凤凰的致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[ ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对");
console.log("答案:" + this.answer3());
}; var TestPaperA = function(){};
TestPaperA.prototype = new TestPaper();
TestPaperA.prototype.answer1 = function(){
return 'b';
};
TestPaperA.prototype.answer2 = function(){
return 'c';
};
TestPaperA.prototype.answer3 = function(){
return 'a';
};
TestPaperA.prototype.init = function(){
this.TestQuestion1();
this.TestQuestion2();
this.TestQuestion3();
}; var TestPaperB = function(){};
TestPaperB.prototype = new TestPaper();
TestPaperB.prototype.answer1 = function(){
return 'c';
};
TestPaperB.prototype.answer2 = function(){
return 'a';
};
TestPaperB.prototype.answer3 = function(){
return 'a';
};
TestPaperB.prototype.init = function(){
this.TestQuestion1();
this.TestQuestion2();
this.TestQuestion3();
}; //调用:
console.log("学生甲抄的试卷:");
var studentA = new TestPaperA();
studentA.init(); console.log("学生乙抄的试卷:");
var studentB = new TestPaperB();
studentB.init();

js语言特性的模板方法:

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();
brew();
pourInCup();
addCondiments();
}; return F;
}; var Coffee = Beverage({
brew:function(){
console.log('用沸水冲泡咖啡');
},
pourInCup:function(){
console.log('把咖啡倒进杯子');
},
addCondiments:function(){
console.log('加糖和牛奶');
}
}); var Tea = Beverage({
brew:function(){
console.log('用沸水浸泡茶叶');
},
pourInCup:function(){
console.log('把茶倒进杯子');
},
addCondiments:function(){
console.log('加柠檬');
}
}); //调用:
var coffee = new Coffee();
coffee.init(); var tea = new Tea();
tea.init();

另一种动态添加原型链的方法,代码如下:

var Upload = function(){
this.uploadingState = new UploadingState(this);
this.pauseState = new PauseState(this);
this.doneState = new DoneState(this);
this.errorState = new ErrorState(this);
} var StateFactory = (function(){
var State = function(){}; State.prototype.clickHandler1 = function(){
throw new Error('子类必须重写父类的clickHandler1方法');
} State.prototype.clickHandler2 = function(){
throw new Error('子类必须重写父类的clickHandler2方法');
} return function(param){ var F = function(uploadObj){
this.uploadObj = uploadObj;
}; F.prototype = new State(); for(var i in param){
F.prototype[i] = param[i];
} return F;
}
})(); var SignState = StateFactory({
clickHandler1:function(){
console.log('扫描中,点击无效...');
},
clickHandler2:function(){
console.log('文件正在上传中,不能删除');
}
}); var UploadingState = StateFactory({
clickHandler1:function(){
this.uploadObj.pause();
},
clickHandler2:function(){
console.log('文件正在上传中,不能删除');
}
}); var PauseState = StateFactory({
clickHandler1:function(){
this.uploadObj.uploading();
},
clickHandler2:function(){
this.uploadObj.del();
}
}); var DoneState = StateFactory({
clickHandler1:function(){
console.log('文件已完成上传,点击无效');
},
clickHandler2:function(){
this.uploadObj.del();
}
}); var ErrorState = StateFactory({
clickHandler1:function(){
console.log('文件上传失败,点击无效');
},
clickHandler2:function(){
this.uploadObj.del();
}
});

js模板方法的更多相关文章

  1. [置顶] js模板方法的思路及实现

    在js中如何实现设计模式中的模板方法? 思路的产生必然要求熟悉js,如何实现?就很简单了,都知道在js中如果定义两个相同名称的方法,前一个方法就会被后一个方法覆盖掉,使用此特点就可以实现模板方法. 例 ...

  2. js 模板方法模式

    * 分离出共同点 function Beverage() {} Beverage.prototype.boilWater = function() { console.log("把水煮沸&q ...

  3. underscore.js 源码阅读 准备

    本次阅读是初次阅读源码,参考了以下几篇文章: https://github.com/hanzichi?language=javascript&page=5&tab=stars http ...

  4. zepto源码研究 - zepto.js - 6(模板方法)

    width  height  模板方法   读写width/height ['width', 'height'].forEach(function(dimension){ //将width,hegih ...

  5. js设计模式——6.模板方法模式与职责链模式

    js设计模式——6.模板方法模式与职责链模式 职责链模式

  6. JS 设计模式七 -- 模板方法模式

    概念 模板方法模式是一直昂只需使用继承就可以实现的非常简单的模式. 模板方法模式由两部分结构组成,第一部分是抽象父类,第二部分是具体实现的子类. 实现 模板方法模式一般的实现方式为继承. // 体育运 ...

  7. js设计模式(七)---模板方法模式

    模板方法模式 模板方法模式是一种只需要继承就可以实现的非常简单的模式. 模板方法模式是由两部分组成,第一部分是抽象父类,第二部分是具体实现的子类, 主要适用在同级的子类具有相同的行为放在父类中实现,而 ...

  8. JS设计模式(8)模板方法模式

    什么是模板方法模式? 定义:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 主要解决:一些方法通用,却在每一个子类都重新 ...

  9. js之模板方法模式

    模板方法模式的定义和组成: 模板方法模式是一种只需使用继承就可以实现的非常简单的模式. 模板方法模式由两部分结构组成,第一部分是抽象父类,第二部分是具体的实现子类.通常在抽象父类中封装了子类的算法框架 ...

随机推荐

  1. angularjs 复选框 单选框

    关于复选框,在做项目的时候,有一下几点心得 单选框 1.判断哪个单选框选中的情况 html代码 判断该复选框是否选中 $scope.agree.isChecked     判断这个值,如果等于1,代表 ...

  2. TestClass必须是public的

    运行一个测试类遇到一下问题: namespace TestSample.Sample {     [TestClass]     class CynthiaTest     {         [Te ...

  3. LOJ#2230. 「BJOI2014」大融合

    LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...

  4. MySQL中myisam和innodb的主键索引有什么区别?

    MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.下图是MyISAM索引的原理图: 这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索 ...

  5. 0x09 Python连接MySQL数据库

    本文介绍Python3连接MySQL的第三方库--PyMySQL的基本使用. PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中 ...

  6. DMR技术白皮书

    DMR技术白皮书 主页(http://pttcn.net):DMR技术白皮书 关于DMR 1.模拟技术的局限性 虽然模拟技术仍具有不少优势,如低廉的成本.可自定的功能以及简便的搭建方式等.但模拟技术已 ...

  7. python基础实例

    1.在Python 语言中,对象是通过引用传递的.在赋值时,不管这个对象是新创建的,还是一个已经存在的,都是将该对象的引用(并不是值)赋值给变量. 如:x=1 1这个整形对象被创建,然后将这个对象的引 ...

  8. MVC4 中使用 Area 和 注意的地方

    在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下:  项目 –> 添加 -> 区域 (Area)  输入 Admin 添加成功后 Area包含:创建一个空 ...

  9. request doesn't contain a multipart/form-data or multipart/mixed stream ……

    有文件控件"file"的表单,在提交的时候,直接使用了ajax提交,结果报了一堆错,原来这个东东要提交表单,还要用post方式,最后更改为: $("#saveForm&q ...

  10. 利用FFmpeg将RTSP转码成RTMP发布在RED5

    安装jdk,并设置环境  from:http://www.w3c.com.cn/%E5%88%A9%E7%94%A8ffmpeg%E5%B0%86-ipcamera-%E7%9A%84rtsp%E8% ...