java decorative mode (Decorator Pattern) details and example code

  • 2020-05-10 18:08:22
  • OfStack

The decorator pattern (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 around an existing class.

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

We use the following example to demonstrate the use of the decorator pattern. Among them, we will decorate 1 shape with different colors without changing the shape class.

implementation

We will create an Shape interface and an entity class that implements the Shape interface. We then create an abstract decorator class, ShapeDecorator, that implements the Shape interface, and use the Shape object as its instance variable.

RedShapeDecorator is an entity class that implements ShapeDecorator.

DecoratorPatternDemo, our demo class decorates Shape objects with RedShapeDecorator.

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

Validate 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
 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: