Problem
When a user interacts with a program, one of the important features is sometimes the ability to undo what was just done. This is common in many applications from graphics to word processing. In fact I just used it now in Microsoft Word. Often it is the case that the operation just executed cannot be reversed appropriately, there is a loss of original information or some other reason. How can we maintain a system that CAN reverse all operations?
Solution
The answer is the memento. A memento is simply a class that contains all information needed to recreate a specific state of an object. When an operation is performed the originator object performs the operation passes back a memento as a side effect. That memento is then stored, and then in the event that the system wishes to return the originator to the old state, then memento is simply passed back to the originator, who adjusts its internal state accordingly. At no time does anyone but the originator know the contents of the memento.
Consequences
Since only the originator know the contents of the memento, no one has to know the implementation details of the originator.
A caretaker object is required to create, store and delete mementos.
Mementos could be very expensive in terms of overhead if the internal state of the originator is complex. So, in some cases mementos might not be a valid solution.