Memento

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
Line 18: Line 18:
  
 
[[image: MementoStructure.png]]
 
[[image: MementoStructure.png]]
 +
 +
==Participants==
 +
===Memento===
 +
This class stores the internal state of the originator and protects it from access by anyone other than the originator. Caretaker can only see part of the Memento interface, just enough to handle it. Originator, however, sees a much wider interface that it can use to restore itself to a previous state.
 +
 +
===Originator===
 +
The originator creates a Memento by taking a snapshot of its state and can later use the Memento to restore its state.
 +
 +
===Caretaker===
 +
The Caretaker keeps the Mementos safe and never looks at the contents of the Memento.
  
  

Revision as of 03:45, 25 July 2009

The Memento pattern is a way to store historical records of the state (or some portion of the state) of an object. A memento can be thought of as a souvenir. Much as a Mickey Mouse soft toy might bring back memories of what Disneyland was like when you visited, a Memento object allows the object which made it to remember what state it was in when the Memento was constructed. Using this information, it can then restore that previous state.

The object whose historical state we are saving is known as the Originator class for the pattern. The goal is to avoid breaking the encapsulation of the Originator. The Memento objects for a particular Originator are stored in a Caretaker class. The Caretaker object is probably the most crucial part of the pattern, as this object allows for the key feature: It stores each Memento without knowledge of its internal details.

Thus the Originator shares (some of) its internal state with the Memento object, but not to any other class. The state information remains fully encapsulated by the Originator, except when creating a Memento. Ideally, the state information in a Memento object could only be accessed by its Originator object, and no other classes (C++ allows for this with "friend" classes, but Java doesn't AFAIK, not sure about C#). In any case, no classes besides the originator need or should use the Originator.

Contents

Use When

Use the Memento pattern when:

  • You need to save a snapshot of an object's state so that you can maybe restore it later, and
  • You don't want to break the encapsulation of an object by adding getters and setters so that its state can be obtained from the outside.

Structure

From the Gang of Four Design Patterns book:

MementoStructure.png

Participants

Memento

This class stores the internal state of the originator and protects it from access by anyone other than the originator. Caretaker can only see part of the Memento interface, just enough to handle it. Originator, however, sees a much wider interface that it can use to restore itself to a previous state.

Originator

The originator creates a Memento by taking a snapshot of its state and can later use the Memento to restore its state.

Caretaker

The Caretaker keeps the Mementos safe and never looks at the contents of the Memento.


See also


Personal tools