友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

12. 装饰器模式

12.1. 定义

根据 GoF 的著名著作 《设计模式》,装饰模式的定义如下:

装饰模式(Decorator)

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

— Erich Gamma、Richard Helm、Ralph Johnson、John Vlissides
《设计模式》

12.2. 类图

Diagram

建造者模式要求建造的过程必须是稳定的

我们需要把所需的功能按正确的顺序串联起来进行控制

Component是定义一个对象接口,可以给这些对象动态地添加职责。ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能[DPE]。

装饰模式是利用SetComponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中[DPE]。

学习模式要善于变通,如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

装饰模式是为已有功能动态地添加更多功能的一种方式。

当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为,

在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,就像你起初的那个‘人’类,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了[DP]。

装饰模式的优点我总结下来就是,把类中的装饰功能从类中搬移去除,这样可以简化原有的类。

这样做更大的好处就是有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

装饰模式的装饰顺序很重要

最理想的情况,是保证装饰类之间彼此独立,这样它们就可以以任意的顺序进行组合了。

代码 76. decorator/Client.java 类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.diguage.didp.decorator;

/**
 * Client 类
 *
 * @author D瓜哥, https://www.diguage.com/
 * @since 2017-05-16
 */
public class Client {
  public static void main(String[] args) {
    Component component = new ConcreteComponent();
    Decorator decoratorA = new ConcreteDecoratorA();
    Decorator decoratorB = new ConcreteDecoratorB();
    decoratorA.setComponent(component);
    decoratorB.setComponent(decoratorA);
    decoratorB.operation();
  }
}
代码 77. decorator/Component.java 类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package com.diguage.didp.decorator;

/**
 * Component 类
 *
 * @author D瓜哥, https://www.diguage.com/
 * @since 2017-05-23 09:09:48
 */
public abstract class Component {
  public abstract void operation();
}
代码 78. decorator/ConcreteComponent.java 类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.diguage.didp.decorator;

/**
 * ConcreteComponent 类
 *
 * @author D瓜哥, https://www.diguage.com/
 * @since 2017-05-23 09:09:48
 */
public class ConcreteComponent extends Component {
  public void operation() {
  }
}
代码 79. decorator/ConcreteDecoratorA.java 类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package com.diguage.didp.decorator;

/**
 * 具体装饰 A 类
 *
 * @author D瓜哥, https://www.diguage.com/
 * @since 2017-05-16
 */
public class ConcreteDecoratorA extends Decorator {

  private String addedState;

  @Override
  public void operation() {
    super.operation();
    addedState = "区别于其他装饰对象的状态值!";
    System.out.println("具体装饰对象 A 的操作");
  }
}
代码 80. decorator/ConcreteDecoratorB.java 类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.diguage.didp.decorator;

/**
 * 具体装饰 B 类
 *
 * @author D瓜哥, https://www.diguage.com/
 * @since 2017-05-16
 */
public class ConcreteDecoratorB extends Decorator {

  private String addedState;

  @Override
  public void operation() {
    super.operation();
    addedState = "区别于其他装饰对象的状态值!";
    addBehavior();
  }

  private void addBehavior() {
    System.out.println("附加的装饰动作!");
  }
}
代码 81. decorator/Decorator.java 类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package com.diguage.didp.decorator;

/**
 * @author D瓜哥, https://www.diguage.com/
 * @since 2017-05-16
 */
public class Decorator extends Component {
  protected Component component;

  public void operation() {
    if (component != null) {
      component.operation();
    }
  }

  public void setComponent(Component component) {
    this.component = component;
  }
}

12.3. 常见示例

  1. java.io.BufferedInputStream

  2. java.io.BufferedOutputStream

  3. java.io.DataInputStream

  4. java.util.zip.ZipOutputStream

  5. java.util.Collections

    1. checkedList(List<E> list, Class<E> type)

    2. checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType)

    3. checkedSet(Set<E> s, Class<E> type)

    4. checkedSortedMap(SortedMap<K, V> m, Class<K> keyType, Class<V> valueType)

    5. checkedSortedSet(SortedSet<E> s, Class<E> type)