The characteristics of

  • Add a new decorator class ShapeDecorator instead of Shape

  • The name of the method remains the same, just adding or modifying the body of the method

  • The ColorShapeDecorator decorator class holds the original object, but with added decorations

    public class ColorShapeDecorator extends ShapeDecorator {

    public ColorShapeDecorator(Shape shape) { super(shape); } @Override public void draw() { setColor(); shape.draw(); } private void setColor() {private void setColor() {Copy the code

    }

The source code

[gitee.com/hankzhousan…

gitee.com

] (link.zhihu.com/?target=htt…).