Bird
Raised Fist0

When is the Decorator Pattern approach better than brute force subclassing?

hard⚖️ Approach Comparison Q8 of Q15
OOP & Design Patterns - Decorator Pattern - Wrapping Behaviour Without Subclassing
Consider two approaches to extending behavior: (1) Brute Force subclassing for every feature combination, and (2) Decorator Pattern using composition. When is the Decorator Pattern approach better than brute force subclassing?
AWhen runtime performance is critical and must be O(1)
BWhen you need flexible, dynamic feature addition without subclass explosion
CWhen the number of feature combinations is small and fixed
DWhen inheritance hierarchies are preferred for clarity
Step-by-Step Solution
Solution:
  1. Step 1: Compare approaches

    Brute force creates many subclasses, Decorator composes features dynamically.
  2. Step 2: Identify trade-offs

    Decorator is better when features combine in many ways and must be added dynamically.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Decorator avoids subclass explosion and supports runtime flexibility [OK]
Quick Trick: Decorator excels with many dynamic feature combinations [OK]
Common Mistakes:
MISTAKES
  • Assuming brute force is always faster
  • Preferring inheritance for dynamic behavior
  • Ignoring subclass explosion problem
Trap Explanation:
PITFALL
  • Candidates often overlook dynamic flexibility benefits of Decorator over subclassing.
Interviewer Note:
CONTEXT
  • Tests understanding of trade-offs between inheritance and composition.
Master "Decorator Pattern - Wrapping Behaviour Without Subclassing" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes