您的位置:首页 > 编程语言 > Java开发

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

2013-01-21 22:28 706 查看
    模板方法就是一个方法(通常为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() {
// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息