Pattern:
Memento


Author

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Intent

"Without violating encapsulation, capture and externalize an objects internal state so that the object can be restored to this state later." (Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley, 1995)

Motivation

Sometimes its necessary to record the internal state of an object. This is required when implementing checkpoints and undo mechanisms that let users back out of tentative operations or recover from errors. You must save state information somewhere so that you can restore objects to their previous states. But objects normally encapsulate some or all of their state, making it inaccessible to other objects and impossible to save externally. Exposing this state would violate encapsulation, which can compromise the applications reliability and extensibility. Consider a graphical editor that supports connectivity between objects. A user can connect two rectangles with a line, and the rectangles stay connected when the user moves either of them. The editor ensures that the line stretches to maintain the connection. The undo mechanism problem may be solved with the Memento pattern. A memento is an object that stores a snapshot of the internal state of another object--the mementos originator. The undo mechanism will request a memento from the originator when it needs to checkpoint the originators state. The originator initializes the memento with information that characterizes its current state. Only the originator can store and retrieve information from the memento--the memento is "opaque" to other objects. A ConstraintSolver (Originator in the thumbnail) can act as an originator. The following events characterize the undo process: (1) The editor requests a memento from the ConstraintSolver as a side-effect of the move operation. (2) The ConstraintSolver creates and returns a memento, an instance of a class SolverState (Memento in the thumbnail) in this case. A SolverState memento contains data structures that describe the current state of the ConstraintSolvers internal equations and variables. (3) Later when the user undoes the move operation, the editor gives the SolverState back to the ConstraintSolver. (4) Based on the information in the SolverState, the ConstraintSolver changes its internal structures to return its equations and variables to their exact previous state.

Known Uses

This pattern has been used on the following systems: Collections in the Dylan language provide an iteration interface that reflects the Memento pattern. These collections use the idea of a "state" object, which is a memento that represents the state of the iteration. The current state of the object may be represented by the collection in any way it chooses as the representation is hidden from the clients. (Apple Computer, Inc., Cupertino, CA. Dylan. An object-oriented dynamic language, 1992.) The QOCA constraint-solving toolkit uses mementos to store incremental information. QOCA relies on a history mechanism to revert to earlier solutions. (Helm, R., T. Huynh, K. Marriott, and J. Vlissides. An object-oriented architecture for constraint-based graphical editing. In Proceedings of the Third Eurographics Workshop on Object-Oriented Graphics, pp. 1-22, Champery, Switzerland, October 1992. Also available as IBM Research Division Technical Report RC 18524 (79392).)

See Also

Command and Iterator patterns

Thumbnail

Keywords

Command pattern, Iterator pattern, Memento pattern, behavioral patterns, Gamma patterns, Token pattern, token, object (state recorded), state recorded, state retrieved, snapshot

Business Domains

any system requiring storage and restoration of object state

Problem Forces

need to temporarily save the state of an object

Benefits

captures object state without violating encapsulation

Liabilities

can be expensive on system resources if large volume of data

Implementation Files

Test driver Test.javaSimple implementation of the pattern - State class. State.javaSimple implementation of the pattern - Originator class. Originator.javaSimple implementation of the pattern - Memento class. Memento.javaSimple implementation of the pattern - Caretaker class. Caretaker.javaTest driver testmemento.cppSimple implementation of the pattern. memento.h



Generated on Fri Oct 20 11:15:50 GMT+02:00 2000 by Framework Studio