设计模式.模板方法模式(Template).pptx_第1页
设计模式.模板方法模式(Template).pptx_第2页
设计模式.模板方法模式(Template).pptx_第3页
设计模式.模板方法模式(Template).pptx_第4页
设计模式.模板方法模式(Template).pptx_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

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

文档简介

设计模式 DesignPattern 张凯副教授计算机学院软件工程系 问题 Problem 题目抄错 会做也白搭 问题 Problem classTestPaperA 试题1publicvoidTestQuestion1 Console WriteLine 杨过得到 后来给了郭靖 炼成倚天剑 屠龙刀的玄铁可能是 a 球磨铸铁b 马口铁c 高速合金钢d 碳素纤维 Console WriteLine 答案 b 试题2publicvoidTestQuestion2 Console WriteLine 杨过 程英 陆无双铲除了情花 造成 a 使这种植物不再害人b 使一种珍稀物种灭绝c 破坏了那个生物圈的生态平衡d 造成该地区沙漠化 Console WriteLine 答案 a 试题3publicvoidTestQuestion3 Console WriteLine 蓝凤凰的致使华山师徒 桃谷六仙呕吐不止 如果你是大夫 会给他们开什么药 a 阿司匹林b 牛黄解毒片c 氟哌酸d 让他们喝大量的生牛奶e 以上全不对 Console WriteLine 答案 c 学生甲的试卷 问题 Problem classTestPaperB 试题1publicvoidTestQuestion1 Console WriteLine 杨过得到 后来给了郭靖 炼成倚天剑 屠龙刀的玄铁可能是 a 球磨铸铁b 马口铁c 高速合金钢d 碳素纤维 Console WriteLine 答案 d 试题2publicvoidTestQuestion2 Console WriteLine 杨过 程英 陆无双铲除了情花 造成 a 使这种植物不再害人b 使一种珍稀物种灭绝c 破坏了那个生物圈的生态平衡d 造成该地区沙漠化 Console WriteLine 答案 b 试题3publicvoidTestQuestion3 Console WriteLine 蓝凤凰的致使华山师徒 桃谷六仙呕吐不止 如果你是大夫 会给他们开什么药 a 阿司匹林b 牛黄解毒片c 氟哌酸d 让他们喝大量的生牛奶e 以上全不对 Console WriteLine 答案 a 学生乙的试卷 问题 Problem staticvoidMain string args Console WriteLine 学生甲的试卷 TestPaperAstudentA newTestPaperA studentA TestQuestion1 studentA TestQuestion2 studentA TestQuestion3 Console WriteLine 学生乙的试卷 TestPaperBstudentB newTestPaperB studentB TestQuestion1 studentB TestQuestion2 studentB TestQuestion3 Console Read 学生甲和学生乙的试卷类非常类似 除了答案不同 没什么区别 如果老师修改题目 2个类都需要修改 问题 Problem classTestPaper 试题1publicvoidTestQuestion1 Console WriteLine 杨过得到 后来给了郭靖 炼成倚天剑 屠龙刀的玄铁可能是 a 球磨铸铁b 马口铁c 高速合金钢d 碳素纤维 Console WriteLine 答案 b 试题2publicvoidTestQuestion2 Console WriteLine 杨过 程英 陆无双铲除了情花 造成 a 使这种植物不再害人b 使一种珍稀物种灭绝c 破坏了那个生物圈的生态平衡d 造成该地区沙漠化 Console WriteLine 答案 a 试题3publicvoidTestQuestion3 Console WriteLine 蓝凤凰的致使华山师徒 桃谷六仙呕吐不止 如果你是大夫 会给他们开什么药 a 阿司匹林b 牛黄解毒片c 氟哌酸d 让他们喝大量的生牛奶e 以上全不对 Console WriteLine 答案 c 试卷父类 问题 Problem 学生甲的试卷 classTestPaperA TestPaper publicnewvoidTestQuestion1 base TestQuestion1 Console WriteLine 答案 c publicnewvoidTestQuestion2 base TestQuestion2 Console WriteLine 答案 b publicnewvoidTestQuestion3 base TestQuestion3 Console WriteLine 答案 a classTestPaperB TestPaper publicnewvoidTestQuestion1 base TestQuestion1 Console WriteLine 答案 d publicnewvoidTestQuestion2 base TestQuestion2 Console WriteLine 答案 c publicnewvoidTestQuestion3 base TestQuestion3 Console WriteLine 答案 a 学生乙的试卷 问题 Problem classTestPaper publicvoidTestQuestion1 Console WriteLine 杨过得到 后来给了郭靖 炼成倚天剑 屠龙刀的玄铁可能是 a 球磨铸铁b 马口铁c 高速合金钢d 碳素纤维 Console WriteLine 答案 Answer1 protectedvirtualstringAnswer1 return 问题 Problem 学生甲抄的试卷classTestPaperA TestPaper protectedoverridestringAnswer1 return b protectedoverridestringAnswer2 return c protectedoverridestringAnswer3 return a 学生乙抄的试卷classTestPaperB TestPaper protectedoverridestringAnswer1 return c protectedoverridestringAnswer2 return a protectedoverridestringAnswer3 return a 问题 Problem 问题 Problem staticvoidMain string args Console WriteLine 学生甲抄的试卷 TestPaperstudentA newTestPaperA studentA TestQuestion1 studentA TestQuestion2 studentA TestQuestion3 Console WriteLine 学生乙抄的试卷 TestPaperstudentB newTestPaperB studentB TestQuestion1 studentB TestQuestion2 studentB TestQuestion3 Console Read 主要内容 模板方法模式 TemplateMethod 模式动机模式名称 模板方法模式 TemplateMethod 准备一个抽象类 将部分逻辑以具体方法以及具体构造函数的形式实现 然后声明一些抽象方法来迫使子类实现剩余的逻辑 不同的子类可以以不同的方式实现这些抽象方法 从而对剩余的逻辑有不同的实现 这就是模版方法模式的用意 模板方法模式 TemplateMethod 模式动机很多人可能没有想到 模版方法模式实际上是所有模式中最为常见的几个模式之一 而且很多人可能使用过模版方法模式而没有意识到自己已经使用了这个模式 模版方法模式是基于继承的代码复用的基本技术 模版方法模式的结构和用法也是面向对象设计的核心 模板方法模式 TemplateMethod 模式动机模版方法模式需要开发抽象类和具体子类的设计师之间的协作 一个设计师负责给出一个算法的轮廓和骨架 另一些设计师则负责给出这个算法的各个逻辑步骤 代表这些具体逻辑步骤的方法称做基本方法 primitivemethod 而将这些基本法方法总汇起来的方法叫做模版方法 templatemethod 这个设计模式的名字就是从此而来 模板方法模式 TemplateMethod 模式定义模板方法 TemplateMethod 定义一个操作中算法的骨架 而将一些步骤延迟到子类中 模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤 模板方法是一种类行为型模式 模板方法模式 TemplateMethod 模式结构 模板方法模式 TemplateMethod 参与者AbstractClass 抽象类ConcreteClass 具体子类 模板方法模式 TemplateMethod 例子 极品飞车中有很多的汽车 但是从操作角度看大同小异 无非是起步 StartUp 行驶 Run 停车 Stop 等行为 结合TemplateMethod模式讲就是在这个程序中 结构 对汽车的操作 是稳定的 但是变化在于各个子步骤 操作行为的具体实现 模板方法模式 TemplateMethod publicabstractclassAbstractCar protectedabstractstringStartUp protectedabstractstringRun protectedabstractstringStop publicvoidDriveOnTheRoad Console WriteLine StartUp Console WriteLine Run Console WriteLine Stop 在这段代码中 抽象方法StartUp Run Stop叫做primitiveoperation 原语操作 它们是在子类中的扩展点 AbstractCar中的DriveOnTheRoad方法叫做templatemethod 模板方法 templatemethod用primitiveoperation定义一个算法 是相对稳定的部分 子类中重新定义primitiveoperation 模板方法模式 TemplateMethod publicclassBORA AbstractCar protectedoverridestringStartUp return BORAisStartUp protectedoverridestringRun return BORAisRunning protectedoverridestringStop return BORAisStoped 模板方法模式 TemplateMethod staticvoidMain string args AbstractCarcar newBORA car DriveOnTheRoad Console Read 模板方法模式 TemplateMethod 模板方法模式的优缺点如下模板方法模式在一个类中形式化地定义算法 而由它的子类实现细节的处理 模板方法模式的优势是 在子类定义详细的处理算法时不会改变算法的结构 模板方法是一种代码复用的基本技术 它们在类库中尤为重要 它们提取了类库中的公共行为 模板方法导致一种反向的控制结构 一个父类调用一个子类的操作 而不是相反 模板方法模式 TemplateMethod 模板方法模式的优缺点如下模板方法的缺点在于每个不同的实现都需要定义一个子类 这会导致类的个数增加 但是更加符合类职责的分配原则 使得类的内聚性得以提高 模板方法模式 TemplateMethod 模式使用在以下情况下可以使用模板方法模式 一次性实现一个算法的不变的部分 并将可变的行为留给子类来实现 各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复 对一些复杂的算法进行分割 将其算法中固定不变的部分设计为模板方法和父类具体方法 而一些可以改变的细节由其子类来实现 模板方法模式 TemplateMethod 模式应用模板方法模式主要应用于框架设计 以确保父类控制处理流程的逻辑顺序 如框架的初始化 模板方法模式 TemplateMethod 模式应用模板方法模式鼓励我们恰当使用继承 此模式可以用来改写一些拥有相同功能的相关类 将可复用的一般性的行为代码移到父类里面 而将特殊化的行为代码移到子类里面 这也进一步说明 虽然继承复用存在一些问题

温馨提示

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

评论

0/150

提交评论