1. 程式人生 > >Java設計模式(九):模板方法設計模式

Java設計模式(九):模板方法設計模式

1. 應用場景

模板模式就是通過抽象類來定義一個邏輯模板,邏輯框架、邏輯原型,然後將無法決定的部分抽象成抽象類交由子類來實現,一般這些抽象類的呼叫邏輯還是在抽象類中完成的。這麼看來,模板就是定義一個框架,比如蓋房子,我們定義一個模板:房子要封閉,有門,有窗等等,但是要什麼樣的門,什麼樣的窗,這些並不在模板中描述,這個交給子類來完善,比如門使用防盜門,窗使用北向的窗等等。

2. 概念

定義演算法框架,並將一些步驟的實現延遲到子類。通過模板方法,子類可以重新定義演算法的某些步驟,而不用改變演算法的結構。

3. Class Digram

在這裡插入圖片描述

這個抽象類中包含了模板方法。這個模板方法所用到的操作的抽象版本。模板方法在實現的過程中,用到了這兩個原語操作。模板方法本身和這兩個操作的具體實現之間解耦了。可能有許多的具體類,每一個都實現了模板方法所需要的全部操作。這個具體類實現抽象的操作,當模板方法需要這裡兩個抽象方法時,會呼叫他們。

4. Implementation

衝咖啡和沖茶都有類似的流程,但是某些步驟會有點不一樣,要求複用那些相同步驟的程式碼。

在這裡插入圖片描述

public abstract class CaffeineBeverageWithHook {
 
	public final void prepareRecipe() {
		boilWater();
		brew();
		pourInCup();
		if (customerWantsCondiments()) {
			addCondiments();
		}
	}
 
	abstract void brew();
 
	abstract void
addCondiments(); public void boilWater() { System.out.println("Boiling water"); } public void pourInCup() { System.out.println("Pouring into cup"); } public boolean customerWantsCondiments() { return true; } } public class CoffeeWithHook extends CaffeineBeverageWithHook{ @Override
void brew() { System.out.println("Dripping Coffee through filter"); } @Override void addCondiments() { System.out.println("Adding Sugar and Milk"); } @Override public boolean customerWantsCondiments() { String answer = getUserInput(); if(answer.toLowerCase().startsWith("y")) return true; else return false; } private String getUserInput(){ String answer=null; System.out.println("Would you like milk and sugar with your coffee (y/n) ?"); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); try { answer=bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } if(answer==null){ return "no"; } return answer; } } public class TeaWithHook extends CaffeineBeverageWithHook { public void brew() { System.out.println("Steeping the tea"); } public void addCondiments() { System.out.println("Adding Lemon"); } public boolean customerWantsCondiments() { String answer = getUserInput(); if (answer.toLowerCase().startsWith("y")) { return true; } else { return false; } } private String getUserInput() { // get the user's response String answer = null; System.out.print("Would you like lemon with your tea (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; } } public class BeverageTestDrive { public static void main(String[] args) { Tea tea = new Tea(); Coffee coffee = new Coffee(); System.out.println("\nMaking tea..."); tea.prepareRecipe(); System.out.println("\nMaking coffee..."); coffee.prepareRecipe(); TeaWithHook teaHook = new TeaWithHook(); CoffeeWithHook coffeeHook = new CoffeeWithHook(); System.out.println("\nMaking tea..."); teaHook.prepareRecipe(); System.out.println("\nMaking coffee..."); coffeeHook.prepareRecipe(); } } 執行結果: Making tea... Boiling water Steeping the tea Pouring into cup Adding Lemon Making coffee... Boiling water Dripping Coffee through filter Pouring into cup Adding Sugar and Milk Making tea... Boiling water Steeping the tea Pouring into cup Would you like lemon with your tea (y/n)? y Adding Lemon Making coffee... Boiling water Dripping Coffee through filter Pouring into cup Would you like milk and sugar with your coffee (y/n) ? n