楼子湾

导航

 

一.定义

装饰者模式(Decorator Pattern):动态的给一个对象添加新的功能,同时又不改变其结构。又叫装饰器模式

若要扩展功能,装饰者模式提供了比继承更有弹性的替代方案

二.装饰者模式的角色

抽象构件(Component)角色:是一个抽象类或者接口,是要装饰原始对象

具体构件(ConcreteComponent)角色:要装饰的实际对象

装饰(Decorator)角色:是一个抽象类,继承和实现Component接口,同时拥有一个对Component实例对象的引用,也可以有自己的方法

具体装饰(ConcreteDecorator)角色:具体的装饰者,负责给ConcreteComponent动态的增加新功能。

UML图

 

 

三.示例

 1 /**
 2  *  抽象构件角色: 要装饰的原始角色
 3  */
 4 public abstract class Component {
 5     public abstract void operate();
 6 }
 7 
 8 /**
 9  *  需要被装饰对象的实现类
10  */
11 public class ConcreteComponent extends Component {
12     @Override
13     public void operate() {
14         System.out.println("ConcreteComponent 实际要被装饰的对象");
15     }
16 }
17 
18 /**
19  *  1.抽象的装饰器类继承或者实现抽象组件类
20  *  2.拥有一个对组件类的引用
21  */
22 public abstract class Decorator extends Component {
23     private Component component;
24 
25     public Decorator(Component component) {
26         this.component = component;
27     }
28 
29     @Override
30     public void operate() {
31         // 调用装饰者方法
32         component.operate();
33     }
34 }
35 /**
36   *  实际装饰者A
37   *
38   */
39 public class ConcreteDecoratorA extends Decorator {
40 
41     public ConcreteDecoratorA(Component component) {
42         super(component);
43     }
44 
45     @Override
46     public void operate() {
47         super.operate();
48         operateAMethod();
49     }
50 
51     public void operateAMethod(){
52         System.out.println("ConcreteDecoratorA 自己的方法");
53     }
54 }
55 
56 /**
57  *  实际装饰者B
58  */
59 public class ConcreteDecoratorB extends Decorator {
60     public ConcreteDecoratorB(Component component) {
61         super(component);
62     }
63 
64     @Override
65     public void operate() {
66         super.operate();
67         operateBMethod();
68     }
69 
70     public void operateBMethod(){
71         System.out.println("ConcreteDecoratorB 自己的方法");
72     }
73 }

测试结果:

1         Component component = new ConcreteComponent();
2         Component componentA = new ConcreteDecoratorA(component);
3         Component componentB = new ConcreteDecoratorB(component);
4         componentA.operate();
5         componentB.operate();

 

 

透明的装饰者和半透明的装饰者

透明的装饰者:要求具体的构建角色、装饰者接口与抽象构建角色的接口一致,这种在使用的时候两种类型没有区别,都是继承或者实现了抽象构建角色, 方法的接口也是完全相同的,可以通过抽象类型的引用去操作,对外是透明的,这种是理想的模式

半透明的装饰者: 如果装饰角色的接口与抽象构建角色的接口不一致,也就是说装饰角色的接口宽一些,装饰角色实际上已经成了一个适配器,这种属于半透明的装饰者模式,这种模式下子类可以有自己的个性化方法。

四.装饰者模式在实际中的应用

1.jdk中的应用

输入流 输出流 字符流 都涉及装饰者模式,每个嵌套层都有自己的特性

1 OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File("user.txt")));
2         InputStream inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("reader.txt"))));

 

 

 

 

 

五.适用场景及优缺点

使用场景:

扩展一个类的功能或者给一个类添加附加职责

优点:

继承的有力补充,比继承灵活,在不改变原有对象的基础上给一个对象扩展新的功能

通过使用不同的装饰类或者这些装饰类的排列组合,可以实现不同效果

符合开闭原则

缺点:

会出现很多装饰类,增加代码复杂性

状态装饰时,多层装饰时会更复杂

posted on 2020-05-06 23:16  楼子湾  阅读(240)  评论(0编辑  收藏  举报