设计模式 (十)装饰模式(Decorator)

装饰模式(Decorator)就是使用被装饰的一个子类的实例,在客户端将这个子类的实例委托给装饰类。装饰模式是结成关系的一个替代方案。

简单的装饰模式是原理图入下:


装饰模式以对客服端透明的方式增添了对象的功能,其在与动态的给对象添加了责任,当然这里的继承便是静态的。

其中重要的地方时装饰对象和真是对象有相同的接口,这样客户端就可以和真是对象一样的交互方式和装饰对象交互,然后装饰对象把所有从客户端接收过来的请求全部转发给真是对象,然后在返还给客户端,这样装饰对象就可以再转发前或者以后添加一些附加功能而不影响对真是对象的操作,这样在不改变原有类的基础想,可以实现对于原有类的这种额外功能的实现,增强了程序的复用性。

同时装饰模式比继承好的地方就是,装饰模式可以动态的对已经存在的类进行任意的组合,实现想要的功能,而继承是静态的实现,不能改变原有类的实现,如果要添加更多的功能,只有添加更多的派生类来实现,这个简洁在下面的例子里对于最后一次打印输出就有很明显的效果,定义好了两个装饰类以后,不用再定义第三个就可以实现两个装饰类排列组合的效果。下面就简单的做了一个通过对于手机接电话的一个扩展,接听电话之前有一个彩铃,接受电话之后会回拨一段广告,这样就出来了下面的例子:


[java]  view plain  copy
  1. package com.designpattern.decorator;  
  2.   
  3. public interface Phone {  
  4.     public void recevieCall(String name);  
  5. }  

[java]  view plain  copy
  1. package com.designpattern.decorator;  
  2.   
  3. public class ChinaMobile implements Phone {  
  4.   
  5.     @Override  
  6.     public void recevieCall(String name) {  
  7.         System.out.println("step recevie " + name + " call");  
  8.     }  
  9.   
  10. }  

[java]  view plain  copy
  1. package com.designpattern.decorator;  
  2.   
  3. public abstract class Decorator implements Phone {  
  4.   
  5.     private Phone phone;  
  6.   
  7.     public Decorator(Phone phone) {  
  8.         this.phone = phone;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void recevieCall(String name) {  
  13.         this.phone.recevieCall(name);  
  14.     }  
  15.   
  16. }  

[java]  view plain  copy
  1. package com.designpattern.decorator;  
  2.   
  3. public class RingBeforeChinaMobileDecorator extends Decorator {  
  4.   
  5.     public RingBeforeChinaMobileDecorator(Phone phone) {  
  6.         super(phone);  
  7.     }  
  8.   
  9.     @Override  
  10.     public void recevieCall(String name) {  
  11.         System.out.println("step ring before recevie " + name + "call");  
  12.         super.recevieCall(name);  
  13.     }  
  14.   
  15. }  

[java]  view plain  copy
  1. package com.designpattern.decorator;  
  2.   
  3. public class AdAfterChinaMobileDecorator extends Decorator {  
  4.   
  5.     public AdAfterChinaMobileDecorator(Phone phone) {  
  6.         super(phone);  
  7.     }  
  8.   
  9.     @Override  
  10.     public void recevieCall(String name) {  
  11.         super.recevieCall(name);  
  12.         System.out.println("step ad after recevie " + name + " call");  
  13.     }  
  14.   
  15. }  

[java]  view plain  copy
  1. package com.designpattern.decorator;  
  2.   
  3. public class Client {  
  4.     public static void main(String[] args) {  
  5.         Phone phone = new ChinaMobile();  
  6.         Decorator decorator = new RingBeforeChinaMobileDecorator(phone);  
  7.         decorator.recevieCall("andimuise");  
  8.         System.out  
  9.                 .println("**************************************************");  
  10.   
  11.         decorator = new AdAfterChinaMobileDecorator(phone);  
  12.         decorator.recevieCall("halberd");  
  13.         System.out  
  14.                 .println("**************************************************");  
  15.   
  16.         decorator = new RingBeforeChinaMobileDecorator(  
  17.                 new AdAfterChinaMobileDecorator(phone));  
  18.         decorator.recevieCall("teacher");  
  19.         System.out  
  20.                 .println("**************************************************");  
  21.     }  
  22. }  
最终输出结果为,很明显在第三次输出的时候我想同时实现两种效果,就把另一个装饰类为为了其中一个装饰类的属性,因为无论是装饰类还是真实类他们是实现的共同的接口,这样就对实现提供了很好的效果
[html]  view plain  copy
  1. step ring before recevie andimuisecall  
  2. step recevie andimuise call  
  3. **************************************************  
  4. step recevie halberd call  
  5. step ad after recevie halberd call  
  6. **************************************************  
  7. step ring before recevie teachercall  
  8. step recevie teacher call  
  9. step ad after recevie teacher call  
  10. **************************************************  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值