The Decorator Pattern is a structural design pattern that allows behavior to be dynamically added to an object * without modifying its original class*.
It wraps an object inside another object (the decorator), which enhances or modifies its behavior at runtime.
- Component → The base interface or abstract class.
- Concrete Component → The main class that implements the component.
- Decorator → A wrapper class that adds new functionality.
- Concrete Decorators → Actual implementations that extend functionality.
✅ When you need to dynamically modify an object’s behavior.
✅ When subclassing is not a flexible option.
✅ When you need to layer multiple enhancements on an object.
- Text Formatting ✍️ (Adding bold, italic, or underline styles to text dynamically)
- Logging 📜 (Adding extra logging functionality to existing classes)
- Coffee Shop Orders ☕ (Adding extra ingredients dynamically to a base coffee order)
✔ Open/Closed Principle – You can extend functionality without modifying existing code.
✔ Flexible Composition – You can combine multiple decorators in different ways.
✔ Runtime Behavior Change – New functionality can be added on the fly.
🔗 Example Code: See Implementation