java设计模式之模板方法模式
2013-01-21 22:28
706 查看
模板方法就是一个方法(通常为final,不允许子类进行覆盖),该方法包含了一个算法的各个步骤(每个步骤相当于一个方法),在模版方法内调用的方法中有抽象的(待子类覆盖),也有实例的。另外在模版方法所属类中可以包含一个钩子方法(hook),该方法可以控制模版方法中的逻辑,该方法提供默认的实现。子类不是必须实现它。下面给出一个具体的例子。
Beverage.java:
Tea.java:
Coffee.java:
TestDrive.java:
打印结果:
Beverage.java:
package org.zjut.pattern; public abstract class Beverage { /* final method which can't be override by subclasses */ /* This is the template method */ public final void prepareRecipe() { boilWater(); brew(); pourInCup(); if(customerWantsCondiments()) addCondiments(); } public abstract void brew(); public abstract void addCondiments(); public void boilWater() { // implementation System.out.println("Boiling water!"); } public void pourInCup() { // implementation System.out.println("Pour in Cup!"); } /* hook */ public boolean customerWantsCondiments() { return true; // default implementation } }
Tea.java:
package org.zjut.pattern; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Tea extends Beverage { @Override public void brew() { // implementation System.out.println("Steep tea in boiling water!"); } @Override public void addCondiments() { // implementation System.out.println("Add lemon!"); } /* override or not depends on customer */ @Override public boolean customerWantsCondiments() { String answer = getuserInput(); if(answer.toLowerCase().startsWith("y")) { return true; } else { return false; } } /* helper method */ private String getuserInput() { String answer = null; System.out.println("Would you like lemon with your tea?"); 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; } }
Coffee.java:
package org.zjut.pattern; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Coffee extends Beverage { @Override public void brew() { System.out.println("Brew coffee in boiling water!"); } @Override public void addCondiments() { System.out.println("Add sugar and milk!"); } /* override or not depends on customer */ @Override public boolean customerWantsCondiments() { String answer = getuserInput(); if(answer.toLowerCase().startsWith("y")) { return true; } else { return false; } } /* helper method */ private String getuserInput() { String answer = null; System.out.println("Would you like sugar and milk with your coffee?"); 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; } }
TestDrive.java:
package org.zjut.pattern; public class TestDrive { public static void main(String... args) { Coffee coffee = new Coffee(); Tea tea = new Tea(); coffee.prepareRecipe(); tea.prepareRecipe(); } }
打印结果:
Boiling water! Brew coffee in boiling water! Pour in Cup! Would you like sugar and milk with your coffee? y Add sugar and milk! Boiling water! Steep tea in boiling water! Pour in Cup! Would you like lemon with your tea? n
相关文章推荐
- Java设计模式之模板方法模式
- java设计模式---模板方法模式
- java设计模式-模板方法模式
- Java设计模式 - 模板方法模式
- java学习笔记-设计模式15(模板方法模式)
- Java设计模式——模板方法模式(TEMPLATE METHOD PATTERN)
- Java设计模式之模板方法模式
- Java设计模式之模板方法模式
- Java设计模式----模板方法模式(Template Method)
- 设计模式--模板方法模式--Java实现-- java访问控制关键字用法
- java设计模式之模板方法模式详解
- Java设计模式之模板方法
- Java设计模式透析之 —— 模板方法(Template Method)
- Java设计模式 Design Pattern:模板方法 Template Method
- JAVA-设计模式之模板方法模式
- Java设计模式之模板方法模式
- Java设计模式透析之 —— 模板方法(Template Method)
- JAVA学习--模板方法设计模式
- Java设计模式之模板方法设计模式——写漂亮的代码————
- 设计模式之模板方法模式(Java实现)