Memento Design Pattern
Behavioral pattern. Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
The Memento design pattern solves problems like: Without violating encapsulation, how can an object's internal state be captured and externalized so that the object can be restored to this state later?
Open in the interactive referenceDownload the PDF reference
Solution
The Memento design pattern provides a solution:
Define
Originator and Memento
objects
so that
an originator saves/restores its internal state to/from a memento.
Describing the Memento design in more detail is the theme
of the following sections.
See Applicability section for all problems Memento can solve.
- The key idea in this pattern is to make an object (originator) itself responsible for saving/restoring its internal state (to/from a memento). Only the originator that created a memento is permitted to access it.
-
Define
OriginatorandMementoobjects:-
Originatordefines an operation for saving its internal state to a memento (createMemento(): return new Memento(state)) and for restoring to a previous state from a memento (restore(memento): state = memento.getState()). -
Mementodefines the required data structures to store an originator's internal state, and it is protected against access by objects other than the originator.
This is usually achieved by implementing memento as inner class of originator and declaring all members of memento private (see Implementation and Sample Code).
-
-
Clients (caretaker) that are responsible for
saving/restoring an originator's
internal state
hold a list of mementos
so that a memento can be passed back to the originator
to restore to a previous state.
But a caretaker isn't permitted to access a memento. Only the originator that created a memento is permitted to access it. This enables to save and restore originator's internal state without violating its encapsulation.
Applicability
Design Problems
-
Saving and Restoring an Object's Internal State
-
Without violating encapsulation,
how can an object's internal state be captured and externalized
so that the object can be restored to this state later?
-
Without violating encapsulation,
Structure
Static Class Structure
-
Caretaker-
Refers to the
Originatorclass to save and restore originator's internal state. -
Holds
Mementoobjects, created and returned by the originator, and passes back a memento to the originator to restore to a previous state. -
Isn't permitted to access a
Mementoobject.
-
Refers to the
-
Originator-
Defines an operation
(
createMemento()) for saving its current internal state to aMementoobject. -
Defines an operation
(
restore(memento)) for restoring to a previous state from a passed inMementoobject.
-
Defines an operation
(
-
Memento- Stores an originator's internal state.
- Only the originator that created a memento can access it.
- This is usually implemented by making memento an inner class of originator and declaring all members of memento private.
- See also Implementation and Sample Code.
Dynamic Object Collaboration
-
This sample scenario
shows (1) saving and (2) restoring
the internal state of an
Originatorobject. -
(1) To save the
Originator's current internal state, theCaretakercallscreateMemento()on theOriginator. -
The
Originatorcreates a newMementoobject, saves its current internal state (setState()), and returns theMementoobject to theCaretaker. -
The
Caretakerholds (takes care of) the returnedMementoobject(s). -
(2) To restore the
Originatorto a previous state, theCaretakercallsrestore(memento)on theOriginatorby providing theMementoobject to be restored. -
The
Originatorgets the state to be restored from the providedMementoobject (getState()). -
See also Sample Code / Example1.
Sample Code 1
Basic Java code for implementing the sample UML diagrams.
1 package com.sample.memento.basic;
2 import java.util.ArrayList;
3 import java.util.List;
4 public class Caretaker {
5 // Running the Caretaker class as application.
6 public static void main(String[] args) {
7 Originator originator = new Originator();
8 Originator.Memento memento; // Memento is inner class of Originator
9 // List of memento objects.
10 List<Originator.Memento> mementos = new ArrayList<Originator.Memento>();
11
12 originator.setState("A");
13 // Saving state.
14 memento = originator.createMemento();
15 mementos.add(memento); // adding to list
16 System.out.println("(1) Saving current state ...... : "
17 + originator.getState());
18 originator.setState("B");
19 // Saving state.
20 memento = originator.createMemento();
21 mementos.add(memento); // adding to list
22 System.out.println("(2) Saving current state ...... : "
23 + originator.getState());
24 // Restoring to previous state.
25 memento = mementos.get(0); // getting previous (first) memento from the list
26 originator.restore(memento);
27 System.out.println("(3) Restoring to previous state : "
28 + originator.getState());
29 }
30 }
(1) Saving current state ...... : A
(2) Saving current state ...... : B
(3) Restoring to previous state : A
1 package com.sample.memento.basic;
2 public class Originator {
3 // Hiding internal state.
4 private String state;
5 // ...
6 // Saving internal state.
7 public Memento createMemento() {
8 Memento memento = new Memento();
9 memento.setState(state);
10 return memento;
11 }
12 // Restoring internal state.
13 void restore(Memento memento) {
14 state = memento.getState();
15 }
16 //
17 public String getState() {
18 return state;
19 }
20 void setState(String state) {
21 this.state = state;
22 }
23 //
24 // Implementing Memento as inner class.
25 // All members are private and accessible only by originator.
26 //
27 public class Memento {
28 // Storing Originator's internal state.
29 private String state;
30 // ...
31 private String getState() {
32 return state;
33 }
34 private void setState(String state) {
35 this.state = state;
36 }
37 }
38 }
Not included on this page: Intent, Problem, Motivation, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.