例子
机器人原有功能:对话、唱歌、放音乐
继承:拖地、跳舞
装饰器:拖地、跳舞
角色:component、decorator、concreteComponent、concreteDecorator
interface Robot{
void doSomething();
}
class FirstRobot implements Robot{
public void doSomething(){
System.out.println("唱歌");
}
}
class DecoratortRobot implements Robot{
private Robot robot;
public DecoratortRobot(Robot robot){
this.robot=robot;
}
public void doSomething(){
System.out.println("唱歌");
}
public void doMoreThing(){
root.doSomeThing();
System.out.println("跳舞");
}
}