Revision Notes/Design Patterns
Field Entry - Design Patterns
Decorator Pattern
Wraps an object to add new behaviors dynamically at runtime without modifying the original class or creating a huge number of subclasses.
Field Note - Real-world Analogy
“Think of dressing up for different weather. You start with your base outfit (the original object). Cold outside? Wrap a jacket on top (Decorator 1). Raining? Add a raincoat on top of that (Decorator 2). You're still you underneath — just with extra layers added dynamically.”
Field Observations - Key Topics Covered
The problem: you want to add optional features to an object. With inheritance you'd create subclasses for every combination: PlainCoffee, CoffeeWithMilk, CoffeeWithSugar, CoffeeWithMilkAndSugar, CoffeeWithVanilla... this explodes combinatorially. The Decorator pattern avoids this entirely.
4 more observations inside
Sign in to read every key point in full
Ready to go deeper?
Open the complete revision note.
Sign in to read every field observation in full, including a runnable code example and side-by-side comparisons with related patterns.
Read Full NotesSee Also in Design Patterns