1 public interface Component { 2 3 void operation(); 4 }
1 public class ConcreteComponent implements Component { 2 3 @Override 4 public void operation() { 5 System.out.println("具体操作的对象"); 6 } 7 }
1 public class Decorator implements Component { 2 3 protected Component component; 4 5 public void setComponent(Component component) { 6 this.component = component; 7 } 8 9 public Component getComponent() { 10 return component; 11 } 12 13 @Override 14 public void operation() { 15 if (null != component) { 16 component.operation(); 17 } 18 } 19 }
1 public class ConcreteDecoratorA extends Decorator { 2 3 @Override 4 public void operation() { 5 super.operation(); 6 System.out.println("A操作装饰"); 7 } 8 }
1 public class ConcreteDecoratorB extends Decorator { 2 3 @Override 4 public void operation() { 5 super.operation(); 6 System.out.println("装饰器B进行操作"); 7 } 8 }
原文:https://www.cnblogs.com/yixiu868/p/11135230.html