Decorator mode
The Decorator Pattern allows adding new functionality to an existing object without changing its structure. This type of design pattern belongs to structural patterns, serving as a wrapper for an existing class.
The Decorator Pattern dynamically modifies an object's behavior by wrapping the object in decorator classes.
This pattern creates a decorator class that wraps the original class and provides additional functionality while preserving the integrity of the class method signatures.
We use the following example to demonstrate the usage of the Decorator pattern. Here, we will decorate a shape with different colors without changing the shape class.
summary
intent
Dynamically add extra responsibilities to an object without changing its structure. The Decorator Pattern offers a flexible alternative to inheritance for extending functionality.
Main problems to be solved
- Avoid introducing static features through inheritance, especially when the number of subclasses is rapidly increasing.
- Allows dynamically adding or modifying functionality of an object at runtime.
Use Case
- When you need to extend a class's functionality without adding a large number of subclasses.
- When you need to dynamically add or revoke an object's functionality.
Implementation Approach
- Define the component interfaceCreate an interface that defines the standard for objects to which responsibilities can be dynamically added.
- Create concrete componentsConcrete classes that implement this interface, providing basic functionality.
- Create an abstract decoratorImplements the same interface, holds a reference to a component interface, and can dynamically add functionality at any time.
- Create concrete decoratorsExtends the abstract decorator, adding extra responsibilities.
Key code
- Component InterfaceDefines the standard for objects that can be decorated.
- ConcreteComponent ClassConcrete classes that implement the Component interface.
- Decorator abstract classImplements the Component interface and contains a reference to a Component interface.
- ConcreteDecorator classExtends the Decorator class, adding additional functionality.
Application example
- Monkey King's 72 transformations: Sun Wukong (ConcreteComponent) gains new abilities through transformation (Decorator).
- Framed decorative painting: A painting (ConcreteComponent) can enhance its display effect by adding glass (ConcreteDecorator) and a frame (ConcreteDecorator).
Advantages
- Low couplingDecorator classes and decorated classes can change independently without affecting each other.
- Flexibility: Can dynamically add or revoke functionality.
- Replace InheritanceProvides a way to extend object functionality outside of inheritance.
Disadvantages
- ComplexityMulti-layer decoration may increase system complexity.
Usage suggestions
- When you need to dynamically extend functionality, consider using the Decorator Pattern.
- Keep the interfaces of decorators and concrete components consistent to ensure flexibility.
Notes
- The Decorator Pattern can replace inheritance, but it should be used cautiously to avoid over-decoration leading to system complexity.
Structure
The Decorator Pattern includes the following core roles:
- Abstract Component: defines the common interface or abstract class for original objects and decorator objects, and can serve as the parent class or interface of concrete component classes.
- Concrete Component: is the original object being decorated, defining the object to which new functionality needs to be added.
- Abstract Decorator: inherits from the abstract component, contains an abstract component object, defines the same interface as the abstract component, and can hold other decorator objects through composition.
- Concrete Decorator: implements the interface of the abstract decorator and is responsible for adding new functionality to the abstract component. Concrete decorators usually perform their own operations before or after calling the original object's methods.
The Decorator Pattern enables multi-level functionality enhancement by nested wrapping of multiple decorator objects. Each concrete decorator class can selectively add new functionality while maintaining consistency of the object interface.
Implementation
We will create aShapeInterface and implementationShapeThe entity class of the interface. Then we create an implementation thatShapeAbstract decorator class for the interface.ShapeDecorator, andShapeobject as its instance variable.
RedShapeDecoratorImplementsShapeDecoratorentity class.
DecoratorPatternDemoClass usageRedShapeDecoratorTo decorateShapeObject.

Step 1
Create an interface:
Shape.java
Step 2
Create an entity class that implements the interface.
Rectangle.java
Circle.java
Step 3
Create an ImplementationShapeAbstract decorator class for the interface.
ShapeDecorator.java
Step 4
Create extendedShapeDecoratorEntity decorator class for the class.
RedShapeDecorator.java
Step 5
UsageRedShapeDecoratorTo decorateShapeObject.
DecoratorPatternDemo.java
Step 6
Execute the program, 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: Redother extensions