Decorator Pattern Step by step

kurmivivek295

Vivek Kurmi

Posted on October 22, 2023

Decorator Pattern Step by step

The Decorator Pattern is a design pattern that allows you to add new behaviors or responsibilities to objects dynamically. Imagine you have a base object, and you want to enhance it without changing its core structure. Let's break it down step by step:

Step 1: Create a Component Interface

  1. Interface/Abstract Class: Start by defining an interface or an abstract class that represents the base object you want to decorate. This is your "Component."
   interface Component {
       String operation();
   }
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Concrete Component

  1. Concrete Component: Implement the interface with a concrete class that represents the basic or plain object. This is your starting point.
   class ConcreteComponent implements Component {
       public String operation() {
           return "This is the core component.";
       }
   }
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Decorator Classes

  1. Decorator Classes: Create decorator classes by implementing the component interface. Decorators wrap the base object and enhance its functionality.
   class Decorator implements Component {
       private Component component;

       public Decorator(Component component) {
           this.component = component;
       }

       public String operation() {
           return component.operation();
       }
   }
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Concrete Decorators

  1. Concrete Decorators: Implement concrete decorator classes by extending the decorator class and adding new behavior. These classes modify the behavior of the base component.
   class ConcreteDecoratorA extends Decorator {
       public ConcreteDecoratorA(Component component) {
           super(component);
       }

       public String operation() {
           return "Decorator A: " + super.operation();
       }
   }

   class ConcreteDecoratorB extends Decorator {
       public ConcreteDecoratorB(Component component) {
           super(component);
       }

       public String operation() {
           return "Decorator B: " + super.operation();
       }
   }
Enter fullscreen mode Exit fullscreen mode

Step 5: Putting It All Together

  1. Usage: In your application, you can create an instance of the base component and then wrap it with one or more decorators to enhance its behavior.
   public class Main {
       public static void main(String[] args) {
           Component component = new ConcreteComponent();
           System.out.println(component.operation());

           Component decoratedA = new ConcreteDecoratorA(component);
           System.out.println(decoratedA.operation());

           Component decoratedB = new ConcreteDecoratorB(component);
           System.out.println(decoratedB.operation());

           Component decoratedAB = new ConcreteDecoratorA(new ConcreteDecoratorB(component));
           System.out.println(decoratedAB.operation());
       }
   }
Enter fullscreen mode Exit fullscreen mode

Output:

This is the core component.
Decorator A: This is the core component.
Decorator B: This is the core component.
Decorator A: Decorator B: This is the core component.
Enter fullscreen mode Exit fullscreen mode

In this step-by-step explanation, we've shown how the Decorator Pattern allows you to add functionality to objects at runtime without modifying their original code, making it a flexible and powerful design pattern in software development.

šŸ˜ If you enjoy the content, please šŸ‘ like, šŸ”„ share, and šŸ‘£ follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

šŸ’– šŸ’Ŗ šŸ™… šŸš©
kurmivivek295
Vivek Kurmi

Posted on October 22, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related