Design Patterns · Middle · Question 7 of 75
How do you implement undo/redo with the Command pattern?
- ✓Correct answer: Keep stacks of commands, each with
executeandundo - 2Save a copy of the whole DOM after every user action
- 3Reload the page and replay all actions except the last one
Explanation
An executed command goes onto the undo stack; undoing calls undo() and moves the command to the redo stack. A new action clears redo. An alternative for immutable state is the Memento pattern: keep previous versions of the state.