java设计模式之模板方法模式

来源:互联网 发布:矩阵等价是什么意思 编辑:程序博客网 时间:2024/05/04 16:36


    模板方法就是一个方法(通常为final,不允许子类进行覆盖),该方法包含了一个算法的各个步骤(每个步骤相当于一个方法),在模版方法内调用的方法中有抽象的(待子类覆盖),也有实例的。另外在模版方法所属类中可以包含一个钩子方法(hook),该方法可以控制模版方法中的逻辑,该方法提供默认的实现。子类不是必须实现它。下面给出一个具体的例子。


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() {// implementationSystem.out.println("Boiling water!");}public void pourInCup() {// implementationSystem.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 {@Overridepublic void brew() {// implementationSystem.out.println("Steep tea in boiling water!");}@Overridepublic void addCondiments() {// implementationSystem.out.println("Add lemon!");}/* override or not depends on customer */@Overridepublic 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 {@Overridepublic void brew() {System.out.println("Brew coffee in boiling water!");}@Overridepublic void addCondiments() {System.out.println("Add sugar and milk!");}/* override or not depends on customer */@Overridepublic 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?yAdd sugar and milk!Boiling water!Steep tea in boiling water!Pour in Cup!Would you like lemon with your tea?n