The decorator pattern (Decorator Pattern) allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern, which is a wrapper of an existing class.
This pattern creates a decoration class that wraps the original class and provides additional functionality while maintaining the integrity of the class method signature.
We demonstrate the use of the decorator pattern through the following example. Among them, we will decorate a shape with different colors without changing the shape class. 意图: Dynamically add some additional responsibilities to an object. In terms of added functionality, the decorator pattern is more flexible than generating subclasses. 主要解决: In general, we often use inheritance to extend a class, because inheritance introduces static features to the class, and with the increase of extended functionality, the subclass expands. 何时使用: Extend the class without adding many subclasses. 如何解决: Divide the specific functions and responsibilities while inheriting the decorator model. 关键代码: 1. The Component class acts as an abstract role and should not be specifically implemented. 2. The decorating class references and inherits the Component class, and the concrete extension class overrides the parent class method. 应用实例: 1. Sun WuKong has 72 changes. when he became a “temple”, he was still a monkey, but he also had the function of a temple. 2. A picture can be hung on the wall with or without a frame, but there is usually a frame, and it is actually a frame hanging on the wall. Before hanging on the wall, the painting can be covered with glass and placed in a frame; then the painting, glass and frame form an object. 优点: The decoration class and the decorated class can develop independently and will not be coupled with each other. The decoration mode is an inherited alternative mode, and the decoration pattern can dynamically expand the function of an implementation class. 缺点: Multi-layer decoration is more complicated. 使用场景: 1. Extend the function of a class. 2. Add function dynamically and cancel dynamically. 注意事项: Can be a substitute for inheritance. We will create a Shape Interface and implementation of the Shape The entity class of the interface. Then we created an implementation Shape Abstract decoration class of interface ShapeDecorator And put Shape Object as its instance variable. RedShapeDecorator It came true. ShapeDecorator The entity class of the DecoratorPatternDemo Class usage RedShapeDecorator To decorate. Shape Object. Create an interface: Create an entity class that implements the interface. Create and implement the Shape The abstract decoration class of the interface. Create an extension ShapeDecorator The entity decoration class of the. Use RedShapeDecorator To decorate. Shape Object. Execute the program and output the result: 6.12.1. Introduction ¶
6.12.2. Realize ¶
6.12.3. Step 1 ¶
Shape.java ¶
publicinterfaceShape{voiddraw();}
6.12.4. Step 2 ¶
Rectangle.java ¶
publicclassRectangleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Shape:
Rectangle");}}
Circle.java ¶
publicclassCircleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Shape:
Circle");}}
6.12.5. Step 3 ¶
ShapeDecorator.java ¶
publicabstractclassShapeDecoratorimplementsShape{protectedShapedecoratedShape;publicShapeDecorator(ShapedecoratedShape){this.decoratedShape=decoratedShape;}publicvoiddraw(){decoratedShape.draw();}}
6.12.6. Step 4 ¶
6.12.7. RedShapeDecorator.java ¶
publicclassRedShapeDecoratorextendsShapeDecorator{publicRedShapeDecorator(ShapedecoratedShape){super(decoratedShape);}@Overridepublicvoiddraw(){decoratedShape.draw();setRedBorder(decoratedShape);}privatevoidsetRedBorder(ShapedecoratedShape){System.out.println("Border
Color: Red");}}
6.12.8. Step 5 ¶
6.12.9. DecoratorPatternDemo.java ¶
publicclassDecoratorPatternDemo{publicstaticvoidmain(String[]args){Shapecircle=newCircle();ShapeDecoratorredCircle=newRedShapeDecorator(newCircle());ShapeDecoratorredRectangle=newRedShapeDecorator(newRectangle());//Shape
redCircle = new RedShapeDecorator(new Circle());//Shape redRectangle =
new RedShapeDecorator(new Rectangle());System.out.println("Circle with
normal border");circle.draw();System.out.println("\\nCircle of red
border");redCircle.draw();System.out.println("\\nRectangle of red
border");redRectangle.draw();}}
6.12.10. Step 6 ¶
Circle with normal border
Shape: Circle
Circle of red border
Shape: Circle
Border Color: Red
Rectangle of red border
Shape: Rectangle
Border Color: Red