装饰者模式详解。
0x00 装饰者模式定义
在不改变基础对象原有功能之上为其动态添加新的功能。
0x01 组成部分
- Component:抽象构件,客户端直接面向它编程。
- Concretecomponent:具体构件,实现了最基础的功能。
- Decorator:抽象装饰者,持有一个抽象构件的引用。
- ConcreteDecorator;具体装饰类,为构件动态添加新功能。

0x10 具体案例
在Java中,如IO流中,就大量使用了装饰者模式,现在举一个MOBA游戏的例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package com.kkk.pattern.Decorator1;
* 抽象构件 * Created by z3jjlzt on 2018/1/9. */ public abstract class Component { public abstract void operator(); }
* 具体构件 * Created by z3jjlzt on 2018/1/9. */ public class ConcreteComponent extends Component{
@Override public void operator() { System.out.println("多兰之剑"); } }
* 抽象装饰者 * Created by z3jjlzt on 2018/1/9. */ public class Decorator extends Component{ private Component component;
public Decorator(Component component) { this.component = component; }
@Override public void operator() { component.operator(); } }
* 具体装饰者 * Created by z3jjlzt on 2018/1/9. */ public class Damage extends Decorator{ public Damage(Component component) { super(component); }
public void operator() { super.operator(); System.out.println(" 加上暴风大剑"); }
}
* 具体装饰者 * Created by z3jjlzt on 2018/1/9. */ public class Crit extends Decorator{ public Crit(Component component) { super(component); }
public void operator() { super.operator(); System.out.println(" 加上无尽之刃"); } }
* 客户端,面向抽象构件编程。 * Created by z3jjlzt on 2018/1/9. */ public class Client { public static void main(String[] args) { Component c1,c2,c3; c1 = new ConcreteComponent(); c2 = new Damage(c1); c3 = new Crit(c2); c2.operator(); c3.operator(); } } 执行结果: 多兰之剑 加上暴风大剑 多兰之剑 加上暴风大剑 加上无尽之刃
|
0x11 模式的分类
重客户端的角度分析,可以分为两类,透明方式以及班透明方式。在透明方式中,客户端对待具体构件和具体装饰者没有任何区别;而在半透明方式中,客户端需要区别对待,以达到能够独立调用具体类中新增的方法的目的。在大多数情况下,应该使用透明方式,符合面向抽象编程的原则。
0xff 总结
- 优点:对于扩展新功能,比继承更为灵活,可以对一对象多次装饰,组合出更加强大的对象。
- 缺点:产生过多小对象,排错更加繁杂。
- 适用场景:在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。