Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Decorator mode


May 27, 2021 Design mode


Table of contents


Decorator mode

Decorator Pattern allows you to add new functionality to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a wrapper for an existing class.

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

Let's demonstrate the use of decorator patterns with the following examples. Among them, we will decorate a shape with different colors without changing the shape class.

Introduced

Intent: Dynamically add some additional responsibilities to an object. In terms of added functionality, decorator mode is more flexible than generating sub-classes.

Main solution: In general, we often use inheritance to implement in order to extend a class, because inheritance introduces static features for the class, and as the extension function increases, the sub-classes expand.

When to use: Extend the class without adding many sub-classes.

How to solve: Divide specific functional responsibilities while inheriting the decorator pattern.

Key code: 1, Component class acts as an abstract role and should not be implemented concretely. 2 , decorate class references and inherit Component classes, specifically extending class override parent methods.

Application example: 1, Sun Wukong has 72 changes, when he became a "temple", he is still a monkey, but he has the function of the temple. 2 , whether or not a picture frame can be hung on the wall, but usually there are frames, and in fact the frame is hung on the wall. B efore hanging on a wall, a painting can be glassed and framed;

Pros: Decorative classes and decorative classes can develop independently, do not coupling with each other, decoration mode is an alternative mode of inheritance, decoration mode can dynamically extend the function of an implementation class.

Cons: Multi-layer decoration is more complex.

Use scenarios: 1, extend the functionality of a class. 2 , dynamic increase function, dynamic undo.

Note: Inheritance can be substituted.

Realize

We'll create a Shape interface and an entity class that implements the Shape interface. Then we create an abstract decorative class ShapeDecorator that implements the Shape interface and use the Shape object as its instance variable.

RedShapeDecorator is an entity class that implements ShapeDecorator.

Decorator Pattern Demo, our demo class uses RedShapeDecorator to decorate Shape objects.

Decorator mode

Step 1

Create an interface.

Shape.java

public interface Shape {
   void draw();
}

Step 2

Create an entity class that implements the interface.

Rectangle.java

public class Rectangle implements Shape {

   @Override
   public void draw() {
      System.out.println("Shape: Rectangle");
   }
}

Circle.java

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

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

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

public class DecoratorPatternDemo {
   public static void main(String[] args) {

      Shape circle = new Circle();

      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

Verify the output.

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red