Bird
Raised Fist0

In a system using Memento pattern, how can you combine it with the Command pattern to implement undo/redo functionality effectively?

hard📝 Trade-off Q9 of Q15
LLD - Behavioral Design Patterns — Part 2
In a system using Memento pattern, how can you combine it with the Command pattern to implement undo/redo functionality effectively?
AUse Mementos to store commands instead of states
BUse Mementos to execute commands directly
CUse Command pattern only, ignoring Mementos
DUse Command objects to execute actions and Mementos to save states before execution
Step-by-Step Solution
Solution:
  1. Step 1: Understand roles of Command and Memento

    Command encapsulates actions; Memento saves object states before changes.
  2. Step 2: Combine for undo/redo

    Before executing a Command, save current state in a Memento. Undo uses Memento to restore state.
  3. Final Answer:

    Use Command objects to execute actions and Mementos to save states before execution -> Option D
  4. Quick Check:

    Command = actions, Memento = state snapshots [OK]
Quick Trick: Commands do actions; Mementos save states [OK]
Common Mistakes:
MISTAKES
  • Confusing Memento with Command
  • Trying to execute commands via Mementos
  • Ignoring state saving before commands

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes