This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Decorator patterns allow new functionality to be added to an existing object without changing its structure. This type of design pattern is a structural pattern and can be used as a wrapper around an existing class.

This pattern creates a decorative class that wraps around the original class and provides additional functionality while preserving the integrity of the class method signature.

introduce

describe parsing
intentions Add additional responsibilities to an object dynamically. The decorator pattern is more flexible than subclassing in terms of adding functionality
Mainly to solve In general, we often use inheritance in order to extend a class, because inheritance introduces static characteristics to the class, and as the extension function increases, the subclass will swell
When to use Extend a class without adding many subclasses.
How to solve Divide specific functional responsibilities while inheriting the decorator pattern.
The key code 1. The Component class plays an abstract role and should not be implemented. 2. Modify the class reference and inherit from the Component class. The extension class overrides the parent class method.
Examples of application 1. Sun Wukong has 72 changes. When he becomes a “temple”, he is still a monkey at the root, but he has the function of a temple. 2. No matter whether a picture has a frame or not, it can be hung on the wall, but usually there is a frame, and in fact the frame is hung on the wall. The picture can be glazed and framed before it is hung on the wall; The picture, the glass and the frame form an object.
advantages The decorator class and the decorator class can develop independently and are not coupled to each other. The decorator pattern is an alternative to inheritance. The decorator pattern can dynamically extend the functionality of an implementation class.
disadvantages Multiple layers are more complicated.
Usage scenarios Extend the functionality of a class. 2, dynamic increase function, dynamic undo.
Matters needing attention In lieu of inheritance.

implementation

This example decorates a shape with different colors without changing the shape class.

Start by creating a Shape interface and an entity class that implements the Shape interface. We then create an abstract decorator class called ShapeDecorator that implements the Shape interface and uses the Shape object as its instance variable.

RedShapeDecorator is an entity class that implements ShapeDecorator.

The DecoratorPatternDemo class uses RedShapeDecorator to decorate Shape objects.

Step 1- Create a graphical interface class

Create a graphical interface:

// Shape.java
public interface Shape {
   void draw();
}
Copy the code

Step 2- Create the graphical implementation class

Create an entity class that implements the interface.

// Rectangle. Java -rectangle public class Rectangle implements Shape {@override public void draw() {system.out.println (" Rectangle ");  Rectangle"); }}Copy the code
// Circle - Circle class implements Shape {@override public void draw() {system.out.println (); Circle"); }}Copy the code

Step 3- Create abstract decorator class

Create an abstract decorator class that implements the Shape interface.

// ShapeDecorator. Java - Graphic decorator class, Public Abstract class ShapeDecorator implements Shape {protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape){ this.decoratedShape = decoratedShape; } public void draw(){ decoratedShape.draw(); }}Copy the code

Step 4- Create an instance of the decorated abstract class

Create an entity decorator class that extends the ShapeDecorator class.

// RedShapeDecorator.java 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"); }}Copy the code

Step 5- Set the scenario class

Use RedShapeDecorator to decorate Shape objects.

// DecoratorPatternDemo.java
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();
   }
}
Copy the code

Step 6- Output results

Execute the program and print 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
Copy the code