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


    模板方法就是一个方法(通常为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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值