装饰者模式(2)

https://blog.csdn.net/pnjlc/article/details/52701929

装饰模式的java实现例子


说明:本文是《大话设计模式》一书的学习文摘和网上相关信息文摘,原书代码例子用C#写,下面用Java改写。

1、装饰模式:在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

2、装饰模式由4种角色组成:
(1)抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加职责的对象。
(2)具体构件(Concrete Component)角色:定义一个将要接收附加职责的类。
(3)装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口,从外类来扩展Component类的功能,但对于Component类来说,是无需知道Decorato的存在的。
(4)具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的职责。

3、装饰模式的UML类图


4、装饰模式的特点
(1)装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
(2)装饰对象包含一个真实对象的引用。
(3)装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
(4)装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。

5、适用性
(1)需要扩展一个类的功能,或给一个类添加附加职责。
(2)需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
(3)需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
(4)当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

6、模式简化
(1)如果只有一个Concrete Component类而没有抽象的Component接口时,可以让Decorator继承Concrete Component。
(2)如果只有一个Concrete Decorator类时,可以将Decorator和Concrete Decorator合并。

7、代码实现

[java] view plain copy
  1. package demo4;  
  2. /** 
  3.  * 抽象构件角色 
  4.  */  
  5. public abstract class Component {  
  6.     public abstract void operation();  
  7. }  
  8.   
  9.   
  10. package demo4;  
  11. /** 
  12.  * 具体构件角色 
  13.  */  
  14. public class ConcreteComponent extends Component {        
  15.     @Override  
  16.     public void operation() {  
  17.         System.out.println("具体对象的操作");  
  18.     }  
  19. }  
  20.   
  21.   
  22. package demo4;  
  23. /** 
  24.  * 装饰角色 
  25.  */  
  26. public abstract class Decorator extends Component {  
  27.     protected Component component;    
  28.     public void setComponent(Component component){  
  29.         this.component = component;  
  30.     }  
  31.       
  32.     @Override  
  33.     public void operation() {  
  34.         if(component != null){  
  35.             component.operation();  
  36.         }  
  37.     }  
  38. }  
  39.   
  40.   
  41. package demo4;  
  42. /** 
  43.  * 具体装饰角色A 
  44.  */  
  45. public class ConcreteDecoratorA extends Decorator {  
  46.     //本类的独有功能  
  47.     private String addedState;  
  48.     //首先运行原Component的operation(),再执行本类的功能,相当于对原Component进行了装饰  
  49.     @Override  
  50.     public void operation(){  
  51.         super.operation();  
  52.         addedState = "New State";  
  53.         System.out.println("具体装饰对象A的操作。" + addedState);  
  54.     }  
  55. }  
  56.   
  57.   
  58. package demo4;  
  59. /** 
  60.  * 具体装饰角色B 
  61.  */  
  62. public class ConcreteDecoratorB extends Decorator {  
  63.     //首先运行原Component的operation(),再执行本类的功能,相当于对原Component进行了装饰  
  64.     @Override  
  65.     public void operation() {  
  66.         super.operation();  
  67.         AddedBehavior();  
  68.         System.out.println("具体装饰对象B的操作");  
  69.     }  
  70.     //本类的独有功能  
  71.     private void AddedBehavior() {  
  72.         System.out.println("我是具体装饰对象B的独有方法");         
  73.     }  
  74. }  
  75.   
  76.   
  77. package demo4;  
  78. /** 
  79.  * 客户端代码 
  80.  */  
  81. public class Demo4 {  
  82.   
  83.     public static void main(String[] args) {  
  84.         ConcreteComponent c = new ConcreteComponent();  
  85.         ConcreteDecoratorA d1 = new ConcreteDecoratorA();  
  86.         ConcreteDecoratorB d2 = new ConcreteDecoratorB();  
  87.           
  88.         d1.setComponent(c);  
  89.         d2.setComponent(d1);  
  90.         d2.operation();  
  91.         /*运行输出结果: 
  92.         具体对象的操作 
  93.         具体装饰对象A的操作。New State 
  94.         我是具体装饰对象B的独有方法 
  95.         具体装饰对象B的操作 
  96.         */  
  97.     }  
  98. }  

8、一个例子:给人搭配不同的服饰

[java] view plain copy
  1. package demo5;  
  2. /** 
  3.  *“Person”类(ConcreteComponent)  
  4.  */  
  5. public class Person {  
  6.   
  7.     public Person() {         
  8.     }  
  9.   
  10.     private String name;  
  11.     public Person(String name){  
  12.         this.name = name;  
  13.     }  
  14.       
  15.     public void show(){  
  16.         System.out.println(String.format("装扮的%s", name));  
  17.     }  
  18. }  
  19.   
  20.   
  21. package demo5;  
  22. /** 
  23.  * 服饰类(Decorator) 
  24.  */  
  25. public class Finery extends Person {  
  26.     protected Person component;  
  27.     public void decorate(Person component) {  
  28.         this.component = component;  
  29.     }  
  30.     @Override  
  31.     public void show() {  
  32.         if(component != null){  
  33.             component.show();  
  34.         }  
  35.     }  
  36. }  
  37.   
  38.   
  39. package demo5;  
  40. /** 
  41.  * 具体服饰类(ConcreteDecorator) 
  42.  */  
  43. public class TShirts extends Finery {  
  44.     @Override  
  45.     public void show() {  
  46.         System.out.println("大T恤");  
  47.         super.show();  
  48.     }  
  49. }  
  50.   
  51.   
  52. package demo5;  
  53. /** 
  54.  * 具体服饰类(ConcreteDecorator) 
  55.  */  
  56. public class Sneakers extends Finery {  
  57.     @Override  
  58.     public void show() {  
  59.         System.out.println("球鞋");  
  60.         super.show();  
  61.     }  
  62. }  
  63.   
  64.   
  65. package demo5;  
  66. /** 
  67.  * 具体服饰类(ConcreteDecorator) 
  68.  */  
  69. public class BigTrouser extends Finery {  
  70.     @Override  
  71.     public void show() {  
  72.         System.out.println("垮裤");  
  73.         super.show();  
  74.     }  
  75. }  
  76.   
  77.   
  78. package demo5;  
  79. /** 
  80.  * 客户端代码 
  81.  */  
  82. public class Demo5 {  
  83.     public static void main(String[] args) {  
  84.         Person xc = new Person("小菜");  
  85.               
  86.         Sneakers pqx = new Sneakers();  
  87.         BigTrouser kk = new BigTrouser();  
  88.         TShirts dtx = new TShirts();  
  89.           
  90.         pqx.decorate(xc);         
  91.         kk.decorate(pqx);  
  92.         dtx.decorate(kk);  
  93.         dtx.show();  
  94.         /* 
  95.         运行结果: 
  96.         大T恤 
  97.         垮裤 
  98.         球鞋 
  99.         装扮的小菜 
  100.          */  
  101.     }  
  102. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值