• Head First设计模式-模板方法模式


    一、整体代码

          CaffeineBeverageWithHook.java

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


            CoffeeWithHook.java

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


           TeaWithHook.java

    import java.io.*;
    
    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;
    	}
    }


            BeverageTestDrive.java

    public class BeverageTestDrive {
    	public static void main(String[] args) { 
    		TeaWithHook teaHook = new TeaWithHook();
    		CoffeeWithHook coffeeHook = new CoffeeWithHook();
     
    		System.out.println("
    Making tea...");
    		teaHook.prepareRecipe();
     
    		System.out.println("
    Making coffee...");
    		coffeeHook.prepareRecipe();
    	}
    }


    二、结果



    三、解释

           1、模板方法模式:在一个方法中定义一个算法的框架,而将步骤子类中。模板方法使得子类在不改变算法结构的情况下,重新定义算法中的某一步骤。

           2、OO原则:别找我,我会找你。由超类主控一切,当他们需要的时候,自动去调用子类。这就和好莱坞一样。

     四、Array.Sort(x implements Comparable);也是模板方法模式。

  • 相关阅读:
    bzoj 1800 & 洛谷 P2165 [AHOI2009]飞行棋 —— 模拟
    bzoj 1050 [ HAOI 2006 ] 旅行comf —— 并查集
    洛谷P2593 [ ZJOI 2006 ] 超级麻将 —— DP
    bzoj 3029 守卫者的挑战 —— 概率DP
    poj 2288 Islands and Bridges ——状压DP
    bzoj 1029 [ JSOI 2007 ] 建筑抢修 —— 贪心
    bzoj 3743 [ Coci 2015 ] Kamp —— 树形DP
    bzoj 1053 [ HAOI 2007 ] 反素数ant ——暴搜
    【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
    构造类斐波那契数列矩阵(矩阵
  • 原文地址:https://www.cnblogs.com/phisy/p/3373731.html
Copyright © 2020-2023  润新知