Bird
Raised Fist0

You need to add multiple optional features to a core object dynamically at runtime without creating a subclass for every possible combination. Which design approach best supports this requirement?

easy🔍 Pattern Recognition Q11 of Q15
OOP & Design Patterns - Decorator Pattern - Wrapping Behaviour Without Subclassing
You need to add multiple optional features to a core object dynamically at runtime without creating a subclass for every possible combination. Which design approach best supports this requirement?
AApply memoization to cache feature combinations for reuse
BCreate a large inheritance hierarchy with subclasses for each feature combination
CUse a greedy algorithm to select features based on priority
DUse composition to wrap the core object with feature objects that add behavior dynamically
Step-by-Step Solution
  1. Step 1: Understand the problem constraints

    The problem requires adding features dynamically without subclass explosion.
  2. Step 2: Identify the design pattern that supports dynamic behavior wrapping

    The Decorator Pattern uses composition to wrap objects and add behavior at runtime, avoiding subclassing for every combination.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Dynamic wrapping via composition avoids subclass explosion [OK]
Quick Trick: Dynamic wrapping avoids subclass explosion [OK]
Common Mistakes:
MISTAKES
  • Thinking subclassing is the only way to add features
  • Confusing greedy algorithms with design patterns
Trap Explanation:
PITFALL
  • Subclassing seems straightforward but leads to combinatorial explosion; greedy and memoization don't address dynamic behavior wrapping.
Interviewer Note:
CONTEXT
  • Tests if candidate recognizes when to use Decorator over subclassing or algorithms.
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