Recall & Review
beginner
What is the main purpose of the Decorator pattern?
The Decorator pattern allows behavior to be added to individual objects dynamically without affecting other objects of the same class. It helps extend functionality in a flexible and reusable way.
Click to reveal answer
beginner
In the Decorator pattern, what role does the 'Component' play?
The Component defines the interface for objects that can have responsibilities added to them dynamically. Both the concrete component and decorators implement this interface.
Click to reveal answer
intermediate
How does a Decorator add new behavior to an object?
A Decorator holds a reference to a Component object and implements the same interface. It forwards requests to the wrapped object and can add extra behavior before or after forwarding.
Click to reveal answer
intermediate
Why is the Decorator pattern preferred over subclassing for adding features?
Decorator pattern avoids an explosion of subclasses by allowing features to be combined at runtime. Subclassing requires creating many classes for every feature combination, which is less flexible.
Click to reveal answer
beginner
Give a real-life example that illustrates the Decorator pattern.
Think of a coffee shop where you order a basic coffee (Component). You can add milk, sugar, or whipped cream (Decorators) to your coffee. Each addition wraps the coffee and adds new behavior (taste, cost) without changing the original coffee.
Click to reveal answer
What does a Decorator object hold a reference to?
✗ Incorrect
A Decorator holds a reference to a Component object to forward requests and add behavior.
Which problem does the Decorator pattern help solve?
✗ Incorrect
Decorator pattern avoids subclass explosion by allowing dynamic feature addition.
In the Decorator pattern, what must decorators implement?
✗ Incorrect
Decorators implement the same interface as the Component to be interchangeable.
Which of these is NOT a benefit of the Decorator pattern?
✗ Incorrect
Decorator pattern does not change original object code; it wraps it.
What is a simple analogy for the Decorator pattern?
✗ Incorrect
Adding toppings to a pizza is like wrapping an object with decorators to add features.
Explain the structure and roles of the main parts in the Decorator pattern.
Think about who defines the interface, who implements it, and who adds behavior.
You got /4 concepts.
Describe a scenario where using the Decorator pattern is better than subclassing.
Consider when you want to add features without creating many new classes.
You got /4 concepts.