设计模式――模板方法模式(Template Method Pattern)
2013-10-27 22:58
771 查看
模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模版方法是的子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
类图
![](http://img1.51cto.com/attachment/201310/230408265.png)
(图片源于网络)
代码实现(Java)
测试代码
运行效果
![](http://img1.51cto.com/attachment/201310/225748270.png)
类图
![](http://img1.51cto.com/attachment/201310/230408265.png)
(图片源于网络)
代码实现(Java)
// CaffeineBeverage.java public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); brew(); pourInCup(); addCondiments(); } abstract void brew(); abstract void addCondiments(); void boilWater() { System.out.println("Boiling water"); } void pourInCup() { System.out.println("Pouring into cup"); } }
// CaffeineBeverageWithHook.java public abstract class CaffeineBeverageWithHook { void prepareRecipe() { boilWater(); brew(); pourInCup(); if (customerWantsCondiments()) { addCondiments(); } } abstract void brew(); abstract void addCondiments(); void boilWater() { System.out.println("Boiling water"); } void pourInCup() { System.out.println("Pouring into cup"); } boolean customerWantsCondiments() { return true; } }
// Coffee.java public class Coffee extends CaffeineBeverage { public void brew() { System.out.println("Dripping Coffee through filter"); } public void addCondiments() { System.out.println("Adding Sugar and Milk"); } }
// CoffeeWithHook.java public class CoffeeWithHook extends CaffeineBeverageWithHook { public void brew() { System.out.println("Dripping Coffee through filter"); } public void addCondiments() { System.out.println("Adding Sugar and Milk"); } public boolean customerWantsCondiments() { String answer = getUserInput(); if (answer.toLowerCase().startsWith("y")) { return true; } else { return false; } } private String getUserInput() { String answer = null; System.out.print("Would you like milk and sugar with your coffee (y/n)? "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { answer = in.readLine(); } catch (IOException ioe) { System.err.println("IO error trying to read your answer"); } if (answer == null) { return "no"; } return answer; } }
// Tea.java public class Tea extends CaffeineBeverage { public void brew() { System.out.println("Steeping the tea"); } public void addCondiments() { System.out.println("Adding Lemon"); } }
// TeaWithHook.java public class TeaWithHook extends CaffeineBeverageWithHook { public void brew() { System.out.println("Steeping the tea"); } public void addCondiments() { System.out.println("Adding Lemon"); } public boolean customerWantsCondiments() { String answer = getUserInput(); if (answer.toLowerCase().startsWith("y")) { return true; } else { return false; } } private String getUserInput() { // get the user's response String answer = null; System.out.print("Would you like lemon with your tea (y/n)? "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { answer = in.readLine(); } catch (IOException ioe) { System.err.println("IO error trying to read your answer"); } if (answer == null) { return "no"; } return answer; } }
测试代码
// BeverageTestDrive.java public class BeverageTestDrive { public static void main(String[] args) { Tea tea = new Tea(); Coffee coffee = new Coffee(); System.out.println("\nMaking tea..."); tea.prepareRecipe(); System.out.println("\nMaking coffee..."); coffee.prepareRecipe(); TeaWithHook teaHook = new TeaWithHook(); CoffeeWithHook coffeeHook = new CoffeeWithHook(); System.out.println("\nMaking tea..."); teaHook.prepareRecipe(); System.out.println("\nMaking coffee..."); coffeeHook.prepareRecipe(); } }
运行效果
![](http://img1.51cto.com/attachment/201310/225748270.png)
相关文章推荐
- 设计模式-行为型-模板方法模式(Template Method Pattern)
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
- 设计模式 - 模板方法模式(template method pattern) Applet 详解
- 设计模式-模板方法模式(Template Method Pattern)
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
- 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
- 极速理解设计模式系列:20.模板方法模式(Template Method Pattern)
- 设计模式 - 模板方法模式(template method pattern) 具体解释
- 极速理解设计模式系列:20.模板方法模式(Template Method Pattern)
- 设计模式——行为型之使用模板方法(Template Method Pattern)模式尽量减少重复相似的代码段(一)
- java设计模式——模板方法模式(Template Method Pattern)
- 设计模式系列(十二)模板方法模式(Template Method Pattern)
- 我所理解的设计模式(C++实现)——模板方法模式(Template Method Pattern)
- c++设计模式:模板方法模式(Template Method Pattern)
- First head 设计模式学习c++实现------模板方法模式(Template method pattern)
- c++设计模式:模板方法模式(Template Method Pattern)
- 设计模式 —— 模板方法模式(Template method Pattern)
- 设计模式拾荒之模板方法模式( Template Method Pattern ): 与工厂方法模式之间的情侣关系
- 深入浅出设计模式——模板方法模式(Template Method Pattern)