当前位置:
文档之家› 第6章 对象设计(9)-模板方法模式
第6章 对象设计(9)-模板方法模式
OO System Analysis & Design
【Template Method Pattern】
2014年11月27日星期四
Neusoft Computer Science and Technology Department copy right
1
Preparing beverages
2014年11月27日星期四
典型的抽象类代码如下所示:
public abstract class AbstractClass { public void templateMethod() //模板方法 { primitiveOperation1(); primitiveOperation2(); primitiveOperation3(); } public void primitiveOperation1() //基本方法—具体方法 { //实现代码 } public abstract void primitiveOperation2(); //基本方法—抽象方法 public void primitiveOperation3() //基本方法—钩子方法 { } }
+prepareRecipe() +boilWater() +brewCoffeeGrinds() +pourInCup() +addSugarAndMilk()
+prepareRecipe() +boilWater() +steepTeaBag() +pourInCup() +addSugarAndMilk()
2014年11月27日星期四 Neusoft Computer Science and Technology Department copy right 19
模式分析
模板方法:一个模板方法是定义在抽象类中 的、把基本操作方法组合在一起形成一个总 算法或一个总行为的方法。 基本方法:基本方法是实现算法各个步骤的 方法,是模板方法的组成部分。
2014年11月27日星期四
Tea +brew() +addCondiments() +customerWantCondiments()
14
Neusoft Computer Science and Technology Department copy right
public abstract class CaffeineBeverageWithHook { void prepareRecipe() { boilWater(); brew(); pourInCup(); if (customerWantsCondiments()) { addCondiments(); } } ………… boolean customerWantsCondiments() { return true; }
Tea +brew() +addCondiments()
Taking the design:
太棒了! 我们已经应用了一 种设计模式-模板 方法模式。
2014年11月27日星期四
Neusoft Computer Science and Technology Department copy right
Boiling some water Brew tea Pour tea in cup Add lemon
3
2014年11月27日星期四
Neusoft Computer Science and Technology Department copy right
根据前面的分析构建相应的类
Coffee Tea
2014年11月27日星期四
Neusoft Computer Science and Technology Department copy right
11
Complete class diagram
primitiveOperation1(); primitiveOperation2();
2014年11月27日星期四
抽象方法(Abstract Method) 具体方法(Concrete Method) 钩子方法(Hook Method):“挂钩”方法和空 方法
2014年11月27日星期四 Neusoft Computer Science and Technology Department copy right 20
Neusoft Computer Science and Technology Department copy right
12
练习1:根据自己的喜好添加调料
请修改前面调制饮料的结构,使其增加钩 子的功能,以便使得用户能够根据自己的 喜好(customerWantCondiments)添 加相应的调料。
模板方法模式
典型的具体子类代码如下所示:
public class ConcreteClass extends AbstractClass { public void primitiveOperation2() { //实现代码 }
public void primitiveOperation3()
CaffeinBeverage +prepareRecipe() +boilWater() +brew() +pourInCup() +addCondiments() +customerWantCondiments()
Coffee +brew() +addCondiments() +customerWantCondiments()
模板方法模式-通用类图
2014年11月27日星期四
Neusoft Computer Science and Technology Department copy right
17
模式分析
模板方法模式是基于继承的代码复用基本技 术,模板方法模式的结构和用法也是面向对 象设计的核心之一。在模板方法模式中,可 以将相同的代码放在父类中,而将不同的方 法实现放在不同的子类中。 模板方法模式是一种类的行为型模式,在它 的结构图中只有类之间的继承关系,没有对 象关联关系。
9
The Template Method Pattern
The Template Method Pattern defines the skeleton of a algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an alห้องสมุดไป่ตู้orithm without changing the algorithm’s structure.
Neusoft Computer Science and Technology Department copy right
2
Let’s start with the recipes.
1. 2. 3. 4. Boiling some water Brew coffee Pour coffee in cup Add sugar and milk 1. 2. 3. 4.
2014年11月27日星期四 Neusoft Computer Science and Technology Department copy right 18
模式分析
在模板方法模式的使用过程中,要求开发抽象类 和开发具体子类的设计师之间进行协作。一个设 计师负责给出一个算法的轮廓和骨架,另一些设 计师则负责给出这个算法的各个逻辑步骤。实现 这些具体逻辑步骤的方法称为基本方法(Primitive Method),而将这些基本法方法汇总起来的方法 称为模板方法(Template Method),模板方法模 式的名字从此而来。
2014年11月27日星期四 Neusoft Computer Science and Technology Department copy right
15
public class TeaWithHook extends CaffeineBeverageWithHook { …… public boolean customerWantsCondiments() { String answer = getUserInput(); if (answer.toLowerCase().startsWith("y")) { return true; } else { return false; } Neusoft Computer Science and 16 2014年11}月27日星期四 Technology Department copy right
模板方法模式
钩子方法(Hook Method)
public void template() { open(); display(); if(isPrint()) { print(); } } public boolean isPrint() { return true; } ……
模板方法模式
Notice that!
① ② ③ ④
Both recipes follow the same algorithm:
Boil some water. Use the hot water to extract the coffee or tea. Pour the resulting beverage into a cup. Add the appropriate condiments to the beverage.
2014年11月27日星期四