DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

1

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay