0x00 装饰者模式定义
在不改变基础对象原有功能之上为其动态添加新的功能。
0x01 组成部分
- Component:抽象构件,客户端直接面向它编程。
- Concretecomponent:具体构件,实现了最基础的功能。
- Decorator:抽象装饰者,持有一个抽象构件的引用。
- ConcreteDecorator;具体装饰类,为构件动态添加新功能。
0x10 具体案例
在Java中,如IO流中,就大量使用了装饰者模式,现在举一个MOBA游戏的例子。
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 总结
- 优点:对于扩展新功能,比继承更为灵活,可以对一对象多次装饰,组合出更加强大的对象。
- 缺点:产生过多小对象,排错更加繁杂。
- 适用场景:在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。