首页 > 其他 > 详细

Decorator Pattern (装饰者模式)

时间:2016-03-19 10:03:52      阅读:266      评论:0      收藏:0      [点我收藏+]

装饰者模式( Decorator Pattern )

意图 : 动态的给一个对象添加一些额外的功能,IO这块内容体现出了装饰模式,Decorator模式相比生成子类更为灵活。

 

角色 :

1)抽象构件角色(Component)--- 定义成一个接口类型

2)具体构件角色 (ConcreteComponent) --- 该类(被装饰者)实现了 Component 接口,

3)装饰角色 (Decorator) --- 该类实现了 Component 接口,并持有 Component接口的引用

4)具体装饰角色 (ConcreteDecorator) --- 该类继承了装饰类

 

UML实现:

技术分享

 

代码实现:

Component.java

 

[java] view plain copy
  1. package com.decorator ;  
  2.   
  3. //抽象构件角色  
  4. public interface Component  
  5. {  
  6.     public void operation() ;  
  7. }  

 

ConcreteComponent.java

 

[java] view plain copy
  1. package com.decorator ;  
  2.   
  3. //具体构件角色  
  4. public class ConcreteComponent implements Component  
  5. {  
  6.     public void operation()  
  7.     {  
  8.         System.out.println("实现功能A") ;  
  9.     }  
  10.   
  11. }  

 

Decorator.java

 

[java] view plain copy
  1. package com.decorator ;  
  2.   
  3. //装饰角色,持有一个构件角色的引用  
  4. public class Decorator implements Component  
  5. {  
  6.     Component component = null ;  
  7.   
  8.     public Decorator(Component component)  
  9.     {  
  10.         this.component = component ;  
  11.     }  
  12.   
  13.     public void operation()  
  14.     {  
  15.         this.component.operation() ;  
  16.     }  
  17. }  

 

ConcreteDecoratorA.java

 

[java] view plain copy
  1. package com.decorator ;  
  2.   
  3. //具体装饰角色A  
  4. public class ConcreteDecoratorA extends Decorator  
  5. {  
  6.     public ConcreteDecoratorA(Component component)  
  7.     {  
  8.         super(component) ;  
  9.     }  
  10.   
  11.     public void operation()  
  12.     {  
  13.         super.operation() ;  
  14.         System.out.println("实现功能B") ;  
  15.     }  
  16.   
  17.   
  18. }  

 

ConcreteDecoratorB.java

 

[java] view plain copy
  1. package com.decorator ;  
  2.   
  3. //具体装饰角色B  
  4. public class ConcreteDecoratorB extends Decorator  
  5. {  
  6.     public ConcreteDecoratorB(Component component)  
  7.     {  
  8.         super(component) ;  
  9.     }  
  10.   
  11.     public void operation()  
  12.     {  
  13.         super.operation() ;  
  14.         System.out.println("实现功能C") ;  
  15.     }  
  16.   
  17.   
  18. }  

 

Client.java

 

[java] view plain copy
  1. package com.decorator ;  
  2.   
  3. public class Client   
  4. {  
  5.     public static void main(String[] args)   
  6.     {  
  7.         //装饰者一般不用出现在客户端 , 因它内部自己会处理  
  8.         //ConcreteComponent cc = new ConcreteComponent() ;  
  9.         //ConcreteDecoratorA cd = new ConcreteDecoratorA(cc) ;  
  10.         //ConcreteDecoratorB cd2 = new ConcreteDecoratorB(cd) ;  
  11.         //cd2.operation() ;  
  12.           
  13.         //上面的代码等价于下面的代码  
  14.         ConcreteDecoratorB cd = new ConcreteDecoratorB(new ConcreteDecoratorA(new ConcreteComponent())) ;  
  15.         cd.operation() ;  
  16.           
  17.     }  
  18. }  

小结:

装饰者和被装饰者拥有共同的接口;

装饰者一般不用客户端去调用 , 因它内部自己会处理;

可以用一个或多个装饰者去包装一个对象,具体装饰类和装饰类可以组合成多种行为;

Decorator Pattern (装饰者模式)

原文:http://www.cnblogs.com/hoobey/p/5294387.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!