Design Patterns: Decorator Pattern

Intent: To dynamically add some additional responsibilities to an object. The decorator pattern is more flexible than subclassing in terms of adding functionality.

Main solution: Generally, in order to extend a class, we often use inheritance to implement it. Since inheritance introduces static features into the class, and with the increase of extended functions, subclasses will be very inflated.

When to use: Extending a class without adding many subclasses.

How to solve it: Divide specific functional responsibilities and inherit the decorator pattern at the same time.

Key code: 1. The Component class acts as an abstract role and should not be implemented concretely. 2. The modified class references and inherits the Component class, and the specific extension class overrides the parent class method.

insert image description here
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 decorator 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 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");
   }
}

Step 5
Use RedShapeDecorator to decorate the Shape object.

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();
   }
}

Guess you like

Origin blog.csdn.net/david2000999/article/details/121613976