🍨 Master Software Design Patterns & Java Architecture!
Enjoying real-world, easy-to-understand explanations? Subscribe to Ram N Java for clean code walkthroughs, design pattern lessons, and software engineering concepts.
🔔 Subscribe to Ram N JavaWhat is the Decorator Design Pattern?
The Decorator Pattern is a structural design pattern that lets you dynamically attach new behaviors or responsibilities to an object at runtime without modifying its underlying code.
Instead of relying on rigid, deep subclassing (inheritance), the Decorator Pattern embraces object composition. It wraps an existing object inside another object that provides extra features while keeping the same interface intact.
Real-Time Analogy: Customizing an Ice Cream
Consider stepping into an ice cream parlor:
- You start with a core Simple Ice Cream base.
- Customer 1 wants to add crunchy Nuts.
- Customer 2 wants both Nuts and Honey.
If you tried solving this with inheritance, you would need to create a dedicated class for every combination imaginable: SimpleIcecreamWithNuts, SimpleIcecreamWithHoney, SimpleIcecreamWithNutsAndHoney, and so on. This quickly explodes into an unmanageable class structure.
With the Decorator Pattern, each topping is a wrapper that simply decorates the base ice cream, layering functionality and costs dynamically!
How the Pattern Works Behind the Scenes
1. Component Interface
Defines the baseline contract common to both the plain ice cream and any added toppings:
public interface Icecream {
String makeIcecream();
}
2. Concrete Component
The baseline implementation representing the plain ice cream:
public class SimpleIcecream implements Icecream {
@Override
public String makeIcecream() {
return "Base Ice Cream";
}
}
3. Decorator Base Class
Implements Icecream and maintains a reference to the wrapped Icecream instance:
public abstract class IcecreamDecorator implements Icecream {
protected Icecream specialIcecream;
public IcecreamDecorator(Icecream specialIcecream) {
this.specialIcecream = specialIcecream;
}
@Override
public String makeIcecream() {
return specialIcecream.makeIcecream();
}
}
4. Concrete Decorators (Toppings)
Each topping wraps the component and adds its own custom behavior:
public class NuttyDecorator extends IcecreamDecorator {
public NuttyDecorator(Icecream specialIcecream) {
super(specialIcecream);
}
@Override
public String makeIcecream() {
return specialIcecream.makeIcecream() + " + Crunchy Nuts";
}
}
public class HoneyDecorator extends IcecreamDecorator {
public HoneyDecorator(Icecream specialIcecream) {
super(specialIcecream);
}
@Override
public String makeIcecream() {
return specialIcecream.makeIcecream() + " + Sweet Honey";
}
}
Why Use the Decorator Pattern?
- Open/Closed Principle: Add new toppings without altering existing ice cream classes.
- Runtime Flexibility: Wrap, stack, and combine decorators in any order.
- Prevents Class Explosion: Eliminates the need to maintain hundreds of permutation classes.
Related Tutorials You Might Like
Continue exploring software design patterns with these related lessons from the channel:
Click here to watch in Youtube : https://www.youtube.com/watch?v=-Q65QWelRZw
| Decorator Design pattern - Real Time Example [Ice Cream] |
See also:
No comments:
Post a Comment