設計模式——模板方法模式

10.

OO原則:好萊塢原則——別找(調用)我,我會(調用)找你。

OO模式:模板方法模式——在一個方法中定義一個算法的骨架,而將一些步驟延遲到子類中。模板方法使得子類可以在不改變算法結構的情況下,重新定義算法中的某些步驟。


例子:沖泡咖啡和茶的準備工作

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");
	}
}


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");
	}
}


public class Tea extends CaffeineBeverage {
	public void brew() {
		System.out.println("Steeping the tea");
	}
	public void addCondiments() {
		System.out.println("Adding Lemon");
	}
}


以下是上述類的關係圖:


從上面我們可以看到,prepareRecipe()就是我們的模板方法,而brew()和addCondiments()需要子類提供方法實現。


若想讓子類實現算法中的可選部分,或讓子類對模板方法中的某些即將發生的步驟做出反應,可加入“鉤子(hook)”方法:

public abstract class CaffeineBeverageWithHook {
 
	final 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;
	}
}



此處的customerWantsCondiments()就是一個“鉤子”方法,它也可以是一個空的缺省實現,不做任何事情,而由子類來選擇是否覆蓋這個方法。


import java.io.*;

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;
	}
}



上面的getUserInput()詢問客戶是否需要加奶和糖,而customerWantsCondiments()調用此方法來實現父類的customerWantsCondiments(),作爲一個條件來控制咖啡的生產過程。


在此比較一下策略模式、模板方法模式、工廠方法模式:

策略模式:封裝可互換的行爲,然後使用委託來決定要採用哪一個行爲;(用組合實現)

模板方法模式:子類決定如何是想算法中的步驟;(用繼承實現)

工廠方法模式:由子類決定實例化那個具體類。(模板方法模式的一個特殊版本)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章