模板方法模式

Template Method

模板方法定义了一个算法的步骤,并允许子类为一个或多个步骤提供实现.
它是一个骨架,在框架的设计中多用此设计模式.

对模板方法进行挂钩:
钩子是一种被声明在抽象类中的方法,但只有空的或者默认的实现.钩子的存在,
可以让子灰有能力对算法的不同点进行挂钩.要不要挂钩,由子类自行决定.

好莱坞原则:
高层组件对低层组件的方式是"别调用我们,我们会调用你".

Java代码  收藏代码
  1. public abstract class CaffeineBeverage {  
  2. //不希望子类覆盖此方法,声明为final    
  3.     final void prepareRecipe() {  
  4.         boilWater();  
  5.         brew();  
  6.         pourInCup();  
  7.         addCondiments();  
  8.     }  
  9.    
  10.     abstract void brew();  
  11.     
  12.     abstract void addCondiments();  
  13.    
  14.     void boilWater() {  
  15.         System.out.println("Boiling water");  
  16.     }  
  17.     
  18.     void pourInCup() {  
  19.         System.out.println("Pouring into cup");  
  20.     }  
  21. }  



Java代码  收藏代码
  1. public class Tea extends CaffeineBeverage {  
  2.     public void brew() {  
  3.         System.out.println("Steeping the tea");  
  4.     }  
  5.     public void addCondiments() {  
  6.         System.out.println("Adding Lemon");  
  7.     }  
  8. }  



Java代码  收藏代码
  1. public class Coffee extends CaffeineBeverage {  
  2.     public void brew() {  
  3.         System.out.println("Dripping Coffee through filter");  
  4.     }  
  5.     public void addCondiments() {  
  6.         System.out.println("Adding Sugar and Milk");  
  7.     }  
  8. }  



对模板方法使用钩子:

Java代码  收藏代码
  1. public abstract class CaffeineBeverageWithHook {  
  2.    
  3.     void prepareRecipe() {  
  4.         boilWater();  
  5.         brew();  
  6.         pourInCup();  
  7.         if (customerWantsCondiments()) {  
  8.             addCondiments();  
  9.         }  
  10.     }  
  11.    
  12.     abstract void brew();  
  13.    
  14.     abstract void addCondiments();  
  15.    
  16.     void boilWater() {  
  17.         System.out.println("Boiling water");  
  18.     }  
  19.    
  20.     void pourInCup() {  
  21.         System.out.println("Pouring into cup");  
  22.     }  
  23.    
  24.     boolean customerWantsCondiments() {  
  25.         return true;  
  26.     }  
  27. }  
  1. import java.io.*;  
  2.   
  3. public class CoffeeWithHook extends CaffeineBeverageWithHook {  
  4.    
  5.     public void brew() {  
  6.         System.out.println("Dripping Coffee through filter");  
  7.     }  
  8.    
  9.     public void addCondiments() {  
  10.         System.out.println("Adding Sugar and Milk");  
  11.     }  
  12.    
  13.     public boolean customerWantsCondiments() {  
  14.   
  15.         String answer = getUserInput();  
  16.   
  17.         if (answer.toLowerCase().startsWith("y")) {  
  18.             return true;  
  19.         } else {  
  20.             return false;  
  21.         }  
  22.     }  
  23.    
  24.     private String getUserInput() {  
  25.         String answer = null;  
  26.   
  27.         System.out.print("Would you like milk and sugar with your coffee (y/n)? ");  
  28.   
  29.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  30.         try {  
  31.             answer = in.readLine();  
  32.         } catch (IOException ioe) {  
  33.             System.err.println("IO error trying to read your answer");  
  34.         }  
  35.         if (answer == null) {  
  36.             return "no";  
  37.         }  
  38.         return answer;  
  39.     }  
  40. }  s
posted @ 2011-11-28 15:18  Flying Dreams  阅读(137)  评论(0编辑  收藏  举报