设计模式之--装饰器模式的硬编码的静态装饰和链化

首先有一个简单业务接口:

public   interface  IMyBusinessObject { 
      
public  String doExecute(String in); 

 这个接口有一个业务对象类的实现。

public   class  MyBusinessObject  implements  IMyBusinessObject { 
public  String doExecute(String in) { 
System.out.println(
" Here in MyBusinessObject doExecute: input : "   +  in); 
return  in; 


现在想要在这个业对象类的方法doExecute之前与之后增加一些行为,装饰器模式使我们十分容易增加这个功能。现在定义一个抽象类,这个抽象类实现上面的业务接口。
public   abstract   class  ADecorator  implements  IMyBusinessObject { 
      
protected  IMyBusinessObject target; 

      
public   void  setTarget(IMyBusinessObject target) { 
            
this .target  =  target; 
      } 

public  ADecorator(){}; 

      
public  ADecorator(IMyBusinessObject target_) { 
            setTarget(target_); 
      } 

为什么要把设置业务对象的方法放到抽象类中呢,这也是重构的一种方法。就是把子类中设置业务对象的方法提到父类中。术语为方法上移。 现在定义一个从ADedorator扩展来的具体类DebugConcreteDecorator,目的是在我们业务对象被调用之前和之后加入一些调试信息:
public   class  DebugConcreteDecorator  extends  ADecorator { 


      
public  DebugConcreteDecorator(IMyBusinessObject target_) { 
            
super (target_); 
      } 

      
public  String doExecute(String in) { 
            System.out 
                        .println(
" DebugConcreteDecorator: before method : doExecute  " ); 
            String ret 
=  target.doExecute(in); 
            System.out.println(
" DebugConcreteDecorator: after method : doExecute  " ); 
            
return  ret; 
      } 


现在在客户端,我们调用业务对象:
IMyBusinessObject aIMyBusinessObject  =   new  MyBusinessObject(); 
IMyBusinessObject wrappedObject 
=   new  DebugConcreteDecorator( 
aIMyBusinessObject); 
wrappedObject.doExecute(
" Hello World " ); 
输出结果如下:
DebugConcreteDecorator: before method : doExecute 
Here in MyBusinessObject doExecute: input :Hello World 
DebugConcreteDecorator: after method : doExecute 
实际的业务方法调用之前链化装饰器----调用一个装饰器后再调用另一个装饰器。让我们再定义另一个装饰器去展示这个方法:
public   class  AnotherConcreteDecorator  extends  ADecorator { 

      
public  AnotherConcreteDecorator( 
                  IMyBusinessObject target_) { 
            
super (target_); 
      } 

      
public  String doExecute(String in) { 
            System.out 
                        .println(
" AnotherConcreteDecorator: Going to execute method : doExecute " ); 
            in 
=  in  +   "  Modified by AnotherConcreteDecorator "
            String ret 
=  target.doExecute(in); 
            System.out 
                        .println(
" AnotherConcreteDecorator: After execute method : doExecute " ); 
            
return  ret; 
      } 


上面的代码片段通过在业务方法输入的字符参数后增加(" Modified by AnotherConcreteDecorator")字符串,从而实现对其进行了修改。如果我们想链化装饰器,在客户端,我们可以编写如下的代码:

IMyBusinessObject aIMyBusinessObject  =   new  MyBusinessObject(); 
IMyBusinessObject wrappedObject 
=   new  AnotherConcreteDecorator( 
            
new  DebugConcreteDecorator(aIMyBusinessObject)); 
wrappedObject.doExecute(
" Hello World " ); 

在上面的代码片段中,我在创建一个DebugConcreteDecorator实例时,向其传递了一个实际的业务对象实例。然后用一个刚才定义的notherConcreteDecorator实例去包装DebugConcreteDecorator实例。AntherConcreteDecorator首先在输入参数后增加字符串对其进行修改,然后调用DebugConcreteDecorator实例的doExecute()方法。这时,DebugConcreteDecorator会记录doExectute()方法的输出的条目,然后调用doExecute()去调用实际业务对象的doExecute()方法。
它的返回路径以相反的顺序。在从实际的业务对象(MyBusinessObject)doExecute()方法返回之后,DebugConcreteDecorator余下代码将被执行。于是,调用返回至AnotherConcreteDecorator实例并执行余下的部份代码。

输出结果如下:

AnotherConcreteDecorator: Going to execute method : doExecute 
DebugConcreteDecorator: before method : doExecute 
Here in MyBusinessObject doExecute: input :Hello World Modified by AnotherConcreteDecorator 
DebugConcreteDecorator: after method : doExecute 
AnotherConcreteDecorator: After execute method : doExecute 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值