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.