Decorator Pattern Step by step
Vivek Kurmi
Posted on October 22, 2023
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
- 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();
}
Step 2: Create a Concrete Component
- 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.";
}
}
Step 3: Create Decorator Classes
- 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();
}
}
Step 4: Create Concrete Decorators
- 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();
}
}
Step 5: Putting It All Together
- 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());
}
}
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.
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
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
October 23, 2023