1 public class Client { 2 public static void main(String[] args) { 3 Component component=new ConcreteComponent(); 4 component=new ConcreteDecoratorA(component); 5 component=new ConcreteDecoratorB(component); 6 component.doSth(); 7 } 8 } 9 interface Component{ 10 void doSth(); 11 } 12 class ConcreteComponent implements Component{ 13 @Override 14 public void doSth() { 15 System.out.println("This is ConcreteComponent.doSth()"); 16 } 17 } 18 abstract class Decorator implements Component{} 19 class ConcreteDecoratorA extends Decorator{ 20 private Component component; 21 public ConcreteDecoratorA(Component component){ 22 this.component=component; 23 } 24 @Override 25 public void doSth() { 26 concreteDecoratorA_method(); 27 component.doSth(); 28 } 29 public void concreteDecoratorA_method(){ 30 System.out.println("This is ConcreteDecoratorA.concreteDecoratorA_method()"); 31 } 32 } 33 class ConcreteDecoratorB extends Decorator{ 34 private Component component; 35 public ConcreteDecoratorB(Component component){ 36 this.component=component; 37 } 38 @Override 39 public void doSth() { 40 concreteDecoratorB_method(); 41 component.doSth(); 42 } 43 public void concreteDecoratorB_method(){ 44 System.out.println("This is ConcreteDecoratorB.concreteDecoratorB_method()"); 45 } 46 }