🎨 Design Patterns (Gang of Four)
Intermediate
How does the Command pattern support undo/redo?
Answer
The Command pattern enables undo/redo by storing executed command objects in a history stack. Each command implements both execute() and undo() methods. To undo, pop the last command from the history stack and call its undo() method, which reverses the operation. To redo, push the command back and call execute() again. For example, a MoveShapeCommand stores the shape, the old position, and the new position. execute() moves the shape to the new position; undo() moves it back to the old position. Text editors, graphic tools, and spreadsheets all implement undo/redo this way, often maintaining separate undo and redo stacks.
Previous
What are the different types of Proxy?
Next
What is the difference between Bridge and Adapter?
More Design Patterns (Gang of Four) Questions
View all →- Intermediate What is the Template Method design pattern?
- Intermediate What is the Visitor design pattern?
- Intermediate What is the difference between Factory Method and Abstract Factory?
- Intermediate When should you NOT use design patterns?
- Intermediate How are the Gang of Four patterns categorized?