0 votes
in Design Patterns by
What is Decorator Design Pattern?

1 Answer

0 votes
by
Decorator design pattern belongs to the category of structural pattern that lets users add new features to an existing object without modifying the structure. This pattern creates a class called decorator class that acts as a wrapper to the existing class by keeping the signatures of class methods intact. This pattern makes use of abstract classes and interfaces with composition for implementing the wrapper. They are mostly used to apply SRP (Single Responsibility Principle) as we divide functionalities into classes with unique concerns. This pattern is structurally similar to the chain of responsibility pattern. Following are the steps to implement decorator design pattern:

Create an interface and concrete classes that implement this interface.

Create an abstract decorator class that implements the above interface.

Create a concrete decorator class that extends the above abstract class.

Use the concrete decorator class to decorate the interface objects and verify the output.

Let us understand this with the help of an example. Here, we will be creating a Shape Interface and concrete classes- Rectangle and Triangle that implement this Shape interface. We will be creating an abstract decorator class “ShapeDecorator” that implements the Shape interface. We will create RedColorDecorator that extends ShapeDecorator. We will be then using this decorator to implement the functionalities.

Creating “Shape” Interface

   // Shape.java

   public interface Shape {

       void draw();

   }

Create concrete classes Rectangle.java and Triangle.java that implement Shape Interface.

   // Rectangle.java

   public class Rectangle implements Shape {

       // Overriding the draw method

       @Override public void draw()

       {

           System.out.println("Rectangle Drawn...");

       }

   }

   // Triangle.java

   public class Triangle implements Shape {

       // Overriding the draw method

       @Override public void draw()

       {

           System.out.println("Triangle Drawn...");

       }

   }

Create ShapeDecorator abstract class that implements the Shape interface.

   // ShapeDecorator.java

   public abstract class ShapeDecorator implements Shape {

       protected Shape shapeDecorated;

       public ShapeDecorator(Shape shapeDecorated)

       {

           this.shapeDecorated = shapeDecorated;

       }

       public void draw() {

           shapeDecorated.draw();

       }

   }

Create the RedColorDecorator.java class that extends the ShapeDecorator class.

   public class RedColorDecorator extends ShapeDecorator {

       public RedColorDecorator(Shape shapeDecorated)

       {

           super(shapeDecorated);

       }

       @Override

       public void draw()

       {

           shapeDecorated.draw();

           setRedBorder(shapeDecorated);

       }

       private void setRedBorder(Shape shapeDecorated)

       {

           System.out.println("Red color border added...");

       }

   }

Implement the Driver class to demo the decorator class.

   // Driver.java

   public class Driver {

       // Main driver method

       public static void main(String[] args)

       {

       

           Shape triangle = new Triangle();

           Shape redTriangle

               = new RedColorDecorator(new Triangle());

           Shape redRectangle = new RedColorDecorator(new Rectangle());

           // Draw normal triangle

           triangle.draw();

           System.out.println(".........");

           // make the triangle red

           redTriangle.draw();

           System.out.println(".........");

           // make the rectangle red

           redRectangle.draw();

           System.out.println(".........");

       }

   }

Validate the output. The output of the above code would be:

   Triangle Drawn...

   .........

   Triangle Drawn...

   Red color border added...

   .........

   Rectangle Drawn...

   Red color border added...

Related questions

0 votes
asked Jul 24, 2023 in Design Patterns by SakshiSharma
0 votes
asked Aug 12, 2020 in Design Patterns by RShastri
...