6.12. Decorator mode

发布时间 :2025-10-25 12:24:45 UTC      

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.

6.12.1. Introduction

意图: 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.

6.12.2. Realize

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.

装饰器模式的 UML 图

6.12.3. Step 1

Create an interface:

Shape.java

publicinterfaceShape{voiddraw();}

6.12.4. Step 2

Create an entity class that implements the interface.

Rectangle.java

publicclassRectangleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Shape:
Rectangle");}}

Circle.java

publicclassCircleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Shape:
Circle");}}

6.12.5. Step 3

Create and implement the Shape The abstract decoration class of the interface.

ShapeDecorator.java

publicabstractclassShapeDecoratorimplementsShape{protectedShapedecoratedShape;publicShapeDecorator(ShapedecoratedShape){this.decoratedShape=decoratedShape;}publicvoiddraw(){decoratedShape.draw();}}

6.12.6. Step 4

Create an extension ShapeDecorator The entity decoration class of the.

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

Use RedShapeDecorator To decorate. Shape Object.

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

Execute the program and output the result:

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.