抽象构件(Component)角色
public interface DecoratorInter {
public void doSomething();
}
具体构件(ConcreteComponent)角色
public class DecoratorImpl implements DecoratorInter{
@Override
public void doSomething() {
System.out.println("doSomething");
}
}
装饰(Decorator)角色
public class Decorator implements DecoratorInter{
public DecoratorInter decoratorInter;
public Decorator(DecoratorInter decoratorInter) {
this.decoratorInter=decoratorInter;
}
@Override
public void doSomething() {
this.decoratorInter.doSomething();
}
}
具体装饰(ConcreteDecorator)角色
public class DecoratorA extends Decorator{
public DecoratorA(DecoratorInter decoratorInter) {
super(decoratorInter);
}
public void doSomething() {
System.out.println("Decorator A");
super.doSomething();
}
//Decorator A
//Decorator B
//doSomething
public static void main(String[] args) {
DecoratorInter inter = new DecoratorA(new DecoratorB(new DecoratorImpl()));
inter.doSomething();
}
}