Component:抽象组件角色,定义一组抽象的接口,规定这个被装饰组件都有哪些功能
ConcreteComponent:实现这个抽象组件的所有功能
Decorator:装饰器角色,它持有一个Component对象实例的引用,定义一个与抽象组件一致的接口
ConcreteDecorate:具体装饰器实现者,负责实现装饰器角色定义的功能
Component
package design.pattern.decorator; public interface Component { public void run(); }
Decorator
package design.pattern.decorator; public class Decorator implements Component{ private Component component; public Decorator(Component component) { this.component = component; } @Override public void run() { System.out.println("Decorator do sth..."); component.run(); } }
ConcreteComponent
package design.pattern.decorator; public class ConcreteComponent implements Component{ @Override public void run() { System.out.println("ConcreteComponent Run"); } }
Client
package design.pattern.decorator; public class Client { public static void main(String[] args) { new Decorator(new ConcreteComponent()).run(); } }