- 继承模式: 这种方式是静态的, 一定是要写一个新的子类, 对类层级进行拓展
- 装饰器模式(关联机制): 动态的; 拿到一个对象就可以对其进行拓展, 不需要修改原有类的逻辑

package 装饰器模式;
import jdk.nashorn.internal.ir.annotations.Ignore;
import static java.lang.annotation.ElementType.PARAMETER;
/**
 * @author 无法手执玫瑰
 * 2020/11/0023 21:13
 */
public class DecoratorPatter {
    public static void main(String[] args) {
        new RobotDecorator(new FirstRobot()).doMoreThing();
    }
}
interface Robot{
    void doSomething();
}
class FirstRobot implements Robot{
    @Override
    public void doSomething() {
        System.out.println("唱歌");
        System.out.println("对话");
    }
}
class RobotDecorator implements Robot{
    private Robot robot;
    public RobotDecorator(Robot robot) {
        this.robot = robot;
    }
    @Override
    public void doSomething() {
        robot.doSomething();
    }
    public void doMoreThing(){
        robot.doSomething();
        System.out.println("跳舞");
        System.out.println("拖地");
    }
}
原文:https://www.cnblogs.com/wfszmg/p/14027325.html