Welcome

首页 / 软件开发 / 数据结构与算法 / hand first设计模式 -模板方法模式

hand first设计模式 -模板方法模式2012-01-05 javaeye pan_java模板方法模式 : 在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新算法.重新定义算法中的某些步骤.

咖啡因饮料超类

Java代码

/**  * 咖啡因饮料超类  * @author panxiuyan  *  */ public abstract class CaffeinBeverage {   /**  * 制作方法  */  public void perareRecip() {  boilWater();  brew();  pourInCup();  if (customerWantsCondiments()) {   addCondimenes();  }   }   /**  * 冲泡  */  public abstract void brew();   /**  * 添加相关的配料  */  public abstract void addCondimenes();   /**  * 把水烧开  */  public void boilWater() {  System.out.println("Boiling water");  }   /**  * 将饭料倒入杯子  */  public void pourInCup() {   System.out.println("Pouring into cup");   }   /**  * 是否添加相关的配料--勾子程序  * @return  */  public boolean customerWantsCondiments() {  return true;  }  }