Bird
Raised Fist0

To support undoing the last added decorator at runtime in a Decorator Pattern implementation, which design modification is most suitable?

hard🧠 Feature Understanding Q10 of Q15
OOP & Design Patterns - Decorator Pattern - Wrapping Behaviour Without Subclassing
To support undoing the last added decorator at runtime in a Decorator Pattern implementation, which design modification is most suitable?
AUse inheritance to create a new subclass without the last decorator.
BMaintain a stack of decorators and remove the top decorator to undo.
CModify the base object's methods to support undo functionality.
DUse a singleton to track the current decorator state.
Step-by-Step Solution
Solution:
  1. Step 1: Understand undo requirement

    Undo means removing the last added decorator dynamically.
  2. Step 2: Identify suitable structure

    A stack data structure naturally supports push/pop operations for decorators.
  3. Step 3: Evaluate options

    Using inheritance or modifying base object breaks dynamic behavior; singleton is unrelated.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Stack supports last-in-first-out removal [OK]
Quick Trick: Undo last decorator? Use stack to push/pop decorators [OK]
Common Mistakes:
MISTAKES
  • Trying to use subclassing which is static and inflexible.
  • Modifying base object breaks open/closed principle.
  • Singleton pattern unrelated to undoing decorators.
Trap Explanation:
PITFALL
  • Subclassing or modifying base object does not allow dynamic undo at runtime.
Interviewer Note:
CONTEXT
  • Tests understanding of dynamic decorator management and undo capability.
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