Memento Pattern

Summary

  • The Memento pattern is used to to capture an object's internal state and save it externally so that it can be restored later.
  • The Memento pattern is used extensively in scientific computing to save the state of long-running computations; in computer games to save the state of play over a matter of hours or days; and in graphics toolkits to preserve the state of a display while objects are being moved around.

Class diagram: (see Memento Pattern code review)

Memento Pattern class diagram of sample code download

Usage

Use the Memento pattern when:

  • An object's state must be saved to be restored later.
  • It is undesirable to expose the state directly.