Standard approaches to implementing Undo/Redo

The Memento pattern is one of two standard ways of providing Undo/Redo functionality. It stores the state of the application or the part of the application that the Undo/Redo operations should apply to. The other approach is to use include an Undo() method in the Command interface while using the Command pattern. There are obvious benefits to each – one is simpler and less work to set up, and the other requires less memory.

If designing for undo/redo functionality, I would select the Memento pattern by default. I’d only look at the adapted form of the Command pattern if reasons such as memory consumption made the Memento approach unsuitable. Providing an Undo() method for every action available to the user can quickly become cumbersome, and the number and complexity of actions can expand quickly in applications. It’s best to start with the simplest approach possible and change later if additional complexity is required.

[Updated to correct spelling.]

Leave a Reply

Your email address will not be published. Required fields are marked *