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?
AA static method
BA subclass object
CA global variable
DA Component object
✗ Incorrect
A Decorator holds a reference to a Component object to forward requests and add behavior.
Which problem does the Decorator pattern help solve?
AToo many subclasses for feature combinations
BLack of inheritance
CMemory leaks
DSlow database queries
✗ Incorrect
Decorator pattern avoids subclass explosion by allowing dynamic feature addition.
In the Decorator pattern, what must decorators implement?
AThe same interface as the Component
BA different interface
COnly abstract classes
DNo interface
✗ Incorrect
Decorators implement the same interface as the Component to be interchangeable.
Which of these is NOT a benefit of the Decorator pattern?
AAdding behavior at runtime
BAvoiding subclass explosion
CChanging the original object's code
DCombining features flexibly
✗ Incorrect
Decorator pattern does not change original object code; it wraps it.
What is a simple analogy for the Decorator pattern?
AWriting a new recipe
BAdding toppings to a pizza
CBuilding a new oven
DOrdering a plain sandwich only
✗ 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.
Practice
(1/5)
1. What is the main purpose of the Decorator pattern in system design?
easy
A. To create multiple instances of a class
B. To add new features to objects without changing their original code
C. To replace an object with a completely new one
D. To store objects in a database
Solution
Step 1: Understand the Decorator pattern goal
The Decorator pattern is designed to add new behavior or features to objects dynamically without modifying their original code.
Step 2: Compare options with the pattern goal
Options B, C, and D do not describe adding features without changing code. Only To add new features to objects without changing their original code matches the pattern's purpose.
Final Answer:
To add new features to objects without changing their original code -> Option B
Quick Check:
Decorator pattern = add features without code change [OK]
Hint: Decorator adds features by wrapping, not by changing code [OK]
Common Mistakes:
Confusing Decorator with Factory pattern
Thinking it replaces objects instead of wrapping
Assuming it stores data instead of adding behavior
2. Which of the following is the correct way to implement a decorator in a class-based design?
easy
A. Use a global variable to store extra features
B. Modify the original class to include new methods directly
C. Delete the original class and write a new one with extra features
D. Create a new class that wraps the original class and adds new methods
Solution
Step 1: Recall Decorator implementation style
Decorator pattern uses a wrapper class that holds an instance of the original class and adds new behavior.
Step 2: Evaluate each option
Create a new class that wraps the original class and adds new methods correctly describes wrapping with a new class. Options A, B, and C do not follow the decorator pattern principles.
Final Answer:
Create a new class that wraps the original class and adds new methods -> Option D
Quick Check:
Decorator = wrapper class adding methods [OK]
Hint: Decorator wraps original class; never modifies it directly [OK]
Common Mistakes:
Changing original class instead of wrapping
Replacing class instead of extending behavior
Using global variables for decoration
3. Consider this pseudocode using the Decorator pattern:
class Coffee:
def cost(self):
return 5
class MilkDecorator:
def __init__(self, coffee):
self.coffee = coffee
def cost(self):
return self.coffee.cost() + 2
order = MilkDecorator(Coffee())
print(order.cost())
What will be the output of print(order.cost())?
medium
A. 7
B. 5
C. 2
D. Error
Solution
Step 1: Understand the base cost
The Coffee class returns 5 when cost() is called.
Step 2: Calculate decorated cost
The MilkDecorator adds 2 to the coffee cost by calling self.coffee.cost() and adding 2.
Final Answer:
7 -> Option A
Quick Check:
5 + 2 = 7 [OK]
Hint: Decorator adds to base cost by calling wrapped object's method [OK]
Common Mistakes:
Forgetting to call wrapped object's method
Returning only decorator's added cost
Confusing base and added costs
4. In the following code snippet, what is the main issue that prevents the decorator from working correctly?
A. The decorator calls itself recursively causing infinite loop
B. The decorator does not call the wrapped component's execute method
C. The constructor is missing a parameter
D. The print statements are incorrect
Solution
Step 1: Analyze method calls inside execute()
The execute() method calls self.execute(), which calls itself recursively without a base case.
Step 2: Identify correct call to wrapped component
The decorator should call self.component.execute() to delegate the call, not call itself.
Final Answer:
The decorator calls itself recursively causing infinite loop -> Option A
Quick Check:
Recursive call without base case = infinite loop [OK]
Hint: Decorator must call wrapped object's method, not itself [OK]
Common Mistakes:
Calling self.execute() instead of wrapped component
Missing delegation to wrapped object
Ignoring recursion issues
5. You are designing a notification system where messages can be sent via Email, SMS, and Push notifications. You want to add features like encryption and logging dynamically without changing the core notification classes. How would you apply the Decorator pattern here?
hard
A. Use inheritance to create subclasses for every combination of features
B. Add encryption and logging code directly inside each notification class
C. Create decorator classes for encryption and logging that wrap notification objects
D. Store all notifications in a database and process features later
Solution
Step 1: Identify need for dynamic feature addition
Encryption and logging should be added without modifying core notification classes, so dynamic wrapping is needed.
Step 2: Apply Decorator pattern for flexible feature addition
Create decorator classes that wrap notification objects and add encryption or logging behavior dynamically.
Final Answer:
Create decorator classes for encryption and logging that wrap notification objects -> Option C
Quick Check:
Decorator pattern = dynamic wrapping for features [OK]
Hint: Wrap notifications with decorators for features, avoid code changes [OK]
Common Mistakes:
Modifying core classes directly
Using inheritance explosion for feature combos
Ignoring dynamic behavior needs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.