模式Template Method与Strategu模式

 设计模式Template Method,核心理念是工作流程逻辑到抽象超类的集中化,一些具体方法实现交给子类处理

 

/**
 * Template Method模式
 * 超类集中业务逻辑,而子类集中于实现基本超作
 * 
@author  paul
 * @date 2006-8-9
 
*/
public   abstract   class  CaffeineBeverage {
    
/**
     * 业务逻辑流程,final确保不被子类 改变
     * prepareRecipe void
     * 2006-8-9 16:00:57
     
*/
    
final   void  prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }
    
/**
     * 模板方法,有子类集体实现
     * 受保护的方法,调用者不用关心实现的细节
     * brew void
     * 2006-8-9 16:02:17
     
*/
    
protected   abstract   void  brew();
  
    
abstract   void  addCondiments();
 
    
void  boilWater() {
        System.out.println(
" Boiling water " );
    }
  
    
void  pourInCup() {
        System.out.println(
" Pouring into cup " );
    }
}

public   class  Coffee  extends  CaffeineBeverage {
    
public   void  brew() {
        System.out.println(
" Dripping Coffee through filter " );
    }
    
public   void  addCondiments() {
        System.out.println(
" Adding Sugar and Milk " );
    }
}
public   class  Tea  extends  CaffeineBeverage {
    
public   void  brew() {
        System.out.println(
" Steeping the tea " );
    }
    
public   void  addCondiments() {
        System.out.println(
" Adding Lemon " );
    }
}

为了获得更大的灵活性,扩展性,可以用Strategy设计模式替代Tmeplate Method模式
把模板方法(交给子类实现的东西)转移到一个接口中

 

/**
 * Strategu模式,把公用方法抽象成接口
 * 
@author  paul
 * @date 2006-8-9
 
*/
public   abstract   class  Beverage {
    
/**
     * 业务逻辑的实现,final确保不被子类 改变
     * prepareRecipe void
     * 2006-8-9 16:00:57
     
*/
    
final   void  prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }
    
private  ActionInDrinking action;
    
public   void  brew() {
        action.brew();
        
    }
    
public   void  addCondiments(){
        
this .action.addCondiments();
    }
    
// protected abstract void brew();
    
// abstract void addCondiments();
 
    
void  boilWater() {
        System.out.println(
" Boiling water " );
    }
  
    
void  pourInCup() {
        System.out.println(
" Pouring into cup " );
    }

    
public  ActionInDrinking getAction() {
        
return  action;
    }

    
public   void  setAction(ActionInDrinking action) {
        
this .action  =  action;
    }
}
/
public   interface  ActionInDrinking {
  
public   void  brew();
  
public   void  addCondiments();

}
public   class  TeaDrink  implements  ActionInDrinking {

    
public  TeaDrink() {
        
super ();
        
//  TODO Auto-generated constructor stub
    }

    
public   void  brew() {
        
//  TODO Auto-generated method stub
        
//  steeping tea
    }

    
public   void  addCondiments() {
        
//  TODO Auto-generated method stub
        
    }

}
以上例子是为了方便比较和探讨,不是来自实际使用的东西。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值