zoukankan      html  css  js  c++  java
  • 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);也是模板方法模式。

  • 相关阅读:
    poj 3068 Bridge Across Islands
    XidianOJ 1086 Flappy v8
    XidianOJ 1036 分配宝藏
    XidianOJ 1090 爬树的V8
    XidianOJ 1088 AK后的V8
    XidianOJ 1062 Black King Bar
    XidianOJ 1091 看Dota视频的V8
    XidianOJ 1098 突击数论前的xry111
    XidianOJ 1019 自然数的秘密
    XidianOJ 1109 Too Naive
  • 原文地址:https://www.cnblogs.com/phisy/p/3373731.html
Copyright © 2011-2022 走看看