The Memento pattern is a design pattern that permits the current state of an object to be stored without breaking the rules of encapsulation. The originating object can be modified as required but can be restored to the saved state at any time.
- Intent: The intent of Memento pattern is to capture the internal state of an object without violating encapsulation and thus providing a way to restore the object to it's initial state when needed.
Motivation & Applicability: This is a behavioral pattern as it defines a manner for controlling communication between classes or entities. The Memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time. Importantly, the pattern allows for the internal state of an object to be saved without breaking the rules of encapsulation. This design pattern is commonly used to provide an application with "Undo" functionality.
Participants of this design pattern are :
- Memento (Memento)
- Stores internal state of the Originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion.
- Protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface to the Memento -- it can only pass the memento to the other objects. Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator that produces the memento would be permitted to access the memento's internal state
- Originator (SalesProspect)
- Creates a memento containing a snapshot of its current internal state.
- Uses the memento to restore its internal state
- Caretaker (ProspectMemory)
- Is responsible for the memento's safekeeping
- Never operates on or examines the contents of a memento.
RaananPosted Nov 26, 2011, 2:52 PM
Tx :)