设计模式模板方法模式学习教案_第1页
设计模式模板方法模式学习教案_第2页
设计模式模板方法模式学习教案_第3页
设计模式模板方法模式学习教案_第4页
设计模式模板方法模式学习教案_第5页
已阅读5页,还剩23页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、会计学1设计模式模板设计模式模板(mbn)方法模式方法模式第一页,共28页。 class TestPaperB /试题1 public void TestQuestion1() Console.WriteLine( 杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁 可能是 a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 ); Console.WriteLine(答案:d); /试题2 public void TestQuestion2() Console.WriteLine( 杨过、程英、陆无双铲除了情花,造成 a.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态

2、平衡 d.造成该地区沙漠化 ); Console.WriteLine(答案:b); /试题3 public void TestQuestion3() Console.WriteLine( 蓝凤凰的致使华山师徒、桃谷六仙呕吐不止,如果(rgu)你是大夫,会给他们 开什么药 a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对 ); Console.WriteLine(答案:a); 学生(xu sheng)乙的试卷第2页/共28页第二页,共28页。 static void Main(string args) Console.WriteLine(学生(xu sheng)

3、甲的试卷:); TestPaperA studentA = new TestPaperA(); studentA.TestQuestion1(); studentA.TestQuestion2(); studentA.TestQuestion3(); Console.WriteLine(学生(xu sheng)乙的试卷:); TestPaperB studentB = new TestPaperB(); studentB.TestQuestion1(); studentB.TestQuestion2(); studentB.TestQuestion3(); Console.Read(); 学生

4、甲和学生乙的试卷(shjun)类非常类似,除了答案不同,没什么区别,如果老师修改题目,2个类都需要修改。第3页/共28页第三页,共28页。 class TestPaper /试题1 public void TestQuestion1() Console.WriteLine( 杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁 可能是 a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 ); Console.WriteLine(答案:b); /试题2 public void TestQuestion2() Console.WriteLine( 杨过、程英、陆无双铲除了情花,造成 a.使这种植物

5、不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化 ); Console.WriteLine(答案:a); /试题3 public void TestQuestion3() Console.WriteLine( 蓝凤凰的致使华山师徒、桃谷六仙呕吐不止(bzh),如果你是大夫,会给他们 开什么药 a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对 ); Console.WriteLine(答案:c); 试卷(shjun)父类第4页/共28页第四页,共28页。学生(xu sheng)甲的试卷 class TestPaperA :

6、TestPaper public new void TestQuestion1() base.TestQuestion1(); Console.WriteLine(答案(d n):c); public new void TestQuestion2() base.TestQuestion2(); Console.WriteLine(答案(d n):b); public new void TestQuestion3() base.TestQuestion3(); Console.WriteLine(答案(d n):a); class TestPaperB : TestPaper public ne

7、w void TestQuestion1() base.TestQuestion1(); Console.WriteLine(答案:d); public new void TestQuestion2() base.TestQuestion2(); Console.WriteLine(答案:c); public new void TestQuestion3() base.TestQuestion3(); Console.WriteLine(答案:a); 学生乙的试卷第5页/共28页第五页,共28页。 class TestPaper public void TestQuestion1() Cons

8、ole.WriteLine( 杨过得到(d do),后来给了郭靖,炼成倚天剑、屠龙 刀的玄铁可能是 a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 ); Console.WriteLine(答案: + Answer1(); protected virtual string Answer1() return ; 第6页/共28页第六页,共28页。 /学生(xu sheng)甲抄的试卷 class TestPaperA : TestPaper protected override string Answer1() return b; protected override string An

9、swer2() return c; protected override string Answer3() return a; /学生(xu sheng)乙抄的试卷 class TestPaperB : TestPaper protected override string Answer1() return c; protected override string Answer2() return a; protected override string Answer3() return a; 第7页/共28页第七页,共28页。第8页/共28页第八页,共28页。 static void Mai

10、n(string args) Console.WriteLine(学生(xu sheng)甲抄的试卷:); TestPaper studentA = new TestPaperA(); studentA.TestQuestion1(); studentA.TestQuestion2(); studentA.TestQuestion3(); Console.WriteLine(学生(xu sheng)乙抄的试卷:); TestPaper studentB = new TestPaperB(); studentB.TestQuestion1(); studentB.TestQuestion2();

11、 studentB.TestQuestion3(); Console.Read(); 第9页/共28页第九页,共28页。模式动机与定义1模式结构与分析2模式实例与解析3模式效果与应用4第10页/共28页第十页,共28页。第11页/共28页第十一页,共28页。第12页/共28页第十二页,共28页。第13页/共28页第十三页,共28页。第14页/共28页第十四页,共28页。第15页/共28页第十五页,共28页。第16页/共28页第十六页,共28页。极品飞车中有很多的汽车,但是从操作角度看大同小异。无非是起步(qb)(StartUp)、行驶(Run)、停车(Stop)等行为。结合Template M

12、ethod模式讲就是在这个程序中,结构(对汽车的操作)是稳定的,但是变化在于各个子步骤(操作行为的具体实现)。第17页/共28页第十七页,共28页。 public abstract class AbstractCar protected abstract string StartUp(); protected abstract string Run(); protected abstract string Stop(); public void DriveOnTheRoad() Console.WriteLine(StartUp(); Console.WriteLine(Run(); Cons

13、ole.WriteLine(Stop(); 在这段代码(di m)中,抽象方法StartUp、Run、Stop叫做primitive operation(原语操作),它们是在子类中的扩展点。AbstractCar中的DriveOnTheRoad方法(fngf)叫做template method(模板方法(fngf)),template method用primitive operation定义一个算法,是相对稳定的部分。(子类中重新定义primitive operation)。第18页/共28页第十八页,共28页。 public class BORA : AbstractCar protected override string StartUp() return BORA is StartUp; protected override string Run() return BORA is Running; protected override string Stop() return BORA is Stoped; 第19页/共28页第十九页,共28页。static void Main(string args) AbstractCar car = new BORA(); car.DriveOnTheRo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论