Details of java decoration pattern (Decorator Pattern)

  • 2020-05-07 19:52:16
  • OfStack

1. 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 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 demonstrate the use of the decorator pattern with the following example. Among them, we will decorate 1 shape with different colors without changing the shape class.

2. Implementation
we will create an Shape interface and an entity class that implements the Shape interface. Then we 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 1 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
creates 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
decorates Shape objects with RedShapeDecorator.

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
validates 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

I hope this article is helpful for you to learn java programming.


Related articles: