Decorator Pattern

Decorator is a structural pattern that allows a user to add new functionality to an existing object without altering its structure. This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

In the following example there is a Shape interface and concrete classes implementing Shape interface. An abstract ShapeDecorator class also implements Shape interface and has Shape object as its instance variable, with RedShapeDecorator as concrete implementation of ShapeDecorator. DecoratorPatternDemo will use RedShapeDecorator to decorate Shape objects.

Decorator Pattern UML Diagram

Step 1 : Create an interface Shape

public interface Shape {
void draw();
}

Step 2 : Create concrete classes implementing the same interface Shape

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

Step 3 : Create abstract decorator class implementing the Shape interface

public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;

public ShapeDecorator(Shape decoratedShape) {
this.decoratedShape = decoratedShape;
}

@Override
public void draw() {
decoratedShape.draw();
}
}

Step 4 : Create concrete decorator class RedShapeDecorator extending the ShapeDecorator class

public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}

@Override
public void draw() {
super.draw();
setRedBorder(decoratedShape);
}

private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
}

Step 5 : Use the RedShapeDecorator to decorate Shape objects

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

The output will be :

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red

Leave a Reply

Your email address will not be published. Required fields are marked *