定义:动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更加灵活。
1.定义一个接口Interface
2.1.定义一个接口具体实现类concrete
2.2.定义一个装饰器类,里面要定义一个private的接口类型变量,并是构造方法的参数。Decorator
3.1.定义一个装饰器具体类,继承自装饰器类。DecoratorA
3.2.定义别的装饰器具体类,继承自装饰器类。DecoratorB
4.使用:Interface interface = new Concrete();
Interface decoratorA = new DecoratorA(interface);
Interface decoratorB = new DecoratorB(interface);
decoratorA.method();
decoratorA.method();