Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-decorator-pattern.html
More tutorials: Beam cloud - free course
Decorator mode
Serial number | Chapter in text | video |
---|---|---|
1 | Summary | |
2 | Realization |
Please refer to the navigation above for reading
1. overview
Decorator Pattern allows you to add new functionality to an existing object without changing its structure. This type of design pattern belongs to structural pattern, which is a wrapper for existing classes.
This pattern creates a decoration class to wrap the original class, and provides additional functions while maintaining the integrity of class method signature.
Let's demonstrate the use of the decorator pattern with the following example. Among them, we will decorate a shape with different colors without changing the shape class.
Intention:
Add some additional responsibilities to an object dynamically. In terms of added functionality, the decorator pattern is more flexible than generating subclasses.
Main solutions:
In general, we often use inheritance to extend a class. Because inheritance introduces static features into a class, and with the increase of extension functions, subclasses will expand.
When to use:
Extend a class without adding many subclasses.
How to solve:
The specific functional responsibilities will be divided, while inheriting the decorator model.
Key code:
- Component classes act as abstract roles and should not be implemented specifically.
- Modify class references and inherits Component class, and specific extension class overrides parent class method.
Application example:
- Monkey King changed 72 times. When he became a "Temple", he was still a monkey, but he had the function of a temple.
- No matter whether a picture has a frame or not, it can be hung on the wall, but usually it has a frame, and in fact, the frame is hung on the wall. Before hanging on the wall, the painting can be covered with glass and put into the frame; at this time, the painting, glass and frame form an object.
Advantage:
Decoration class and decorated class can develop independently without coupling each other. Decoration mode is an inherited alternative mode. Decoration mode can dynamically expand the function of an implementation class.
Disadvantages:
The multi-layer decoration is more complex.
Usage scenario:
- Extend the functionality of a class.
- Dynamic add function, dynamic undo.
matters needing attention:
Instead of inheritance.
2. implementation
We will create a Shape interface and an entity class that implements the Shape interface.
Then we create an abstract decoration class ShapeDecorator that implements the Shape interface, and take the Shape object as its instance variable.
RedShapeDecorator is an entity class that implements ShapeDecorator.
DecoratorPatternDemo, our demo class uses RedShapeDecorator to decorate Shape objects.
Step 1
Create an interface.
Shape.java, the code is as follows:
public interface Shape { void draw(); }
Step 2
Create the entity class that implements the interface.
Rectangle.java, the code is as follows:
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Shape: Rectangle"); } }
Circle.java, the code is as follows:
public class Circle implements Shape { @Override public void draw() { System.out.println("Shape: Circle"); } }
Step 3
Create an abstract decoration class that implements the Shape interface.
ShapeDecorator.java, the code is as follows:
public abstract class ShapeDecorator implements Shape { protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape){ this.decoratedShape = decoratedShape; } public void draw(){ decoratedShape.draw(); } }
Step 4
Create an entity decoration class that extends the ShapeDecorator class.
RedShapeDecorator.java, the code is as follows:
public class RedShapeDecorator extends ShapeDecorator { public RedShapeDecorator(Shape decoratedShape) { super(decoratedShape); } @Override public void draw() { decoratedShape.draw(); setRedBorder(decoratedShape); } private void setRedBorder(Shape decoratedShape){ System.out.println("Border Color: Red"); } }
Step 5
Use RedShapeDecorator to decorate Shape objects.
DecoratorPatternDemo.java, the code is as follows:
public class DecoratorPatternDemo { public static void main(String[] args) { Shape circle = new Circle(); ShapeDecorator redCircle = new RedShapeDecorator(new Circle()); ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle()); //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(); } }
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
Last article: Combination mode
Next article: Appearance mode