Memento
Memento
Memento
Behavioral Pattern
Memento
• Memento : to remember ; an object given or kept as a
reminder or in memory of somebody or something
• Sometimes it's necessary to record the internal state of an
object
– E.g., when implementing checkpoints and undo
mechanisms
• lets users back out of tentative operations or recover from
errors
– in database transactions, for rollback incase
transaction fails or is incomplete
• Here, we have to save state information so that objects
can be restored to their previous states
Design Patterns 3
Memento Pattern
• Problem
– State information has to be saved somewhere so that
objects can be restored to their previous states
• However, 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 application's reliability and extensibility
• Memento pattern can be used to solve this problem
– Intent
• Without violating encapsulation, capture and externalize an
object's internal state so that the object can be restored to
this state later
– Also Known As: Token
Design Patterns 4
Memento Pattern
• The Memento pattern attempts to solve this problem by
having privileged access to the state of the object you
want to save
– Other objects have only a more restricted access to the object,
thus preserving their encapsulation
Step-I: Originator’s state can be saved by a Memento object that is created by the
Originator. After creation, Originator sets the state of the Memento
Step-II: Originator can be restored to a given state by passing a Memento object in the
‘setMemento()’ method. The state of the Memento will become the state of the
Originator
Design Patterns 6
Collaborations
• A caretaker requests a memento from an originator, holds it for a time,
and passes it back to the originator
– following interaction diagram illustrates this process:
• Java:
– Option I : Using ‘default’ or ‘package-private’ access level
• Only modules in same package can access
– Option II : Make Memento as a private inner class of the Originator
• Inner class can access ALL members of the outer class including private
Design Patterns 8
C++: ‘friend’ Example
• In the following example,
– the friend function print() is a member of class Y and accesses the private data
members a and b of class X
class X;
class Y { int main() {
public:
void print(X& x); X xobj;
}; Y yobj;
class X {
int a, b; yobj.print(xobj);
friend void Y::print(X& x); }
public:
X() : a(1), b(2) { }
};
void Y::print (X& x) { Method of class Y can access the private
cout << "a is " << x.a << endl; member variables ‘a’ & ‘b’ of another class
cout << "b is " << x.b << endl; X because it is declared as ‘friend’ in X
}
Design Patterns 9
Java : Memento as inner class
MementoExample.java
Design Patterns 10
Narrow & Wide Interfaces
• An object can export multiple interfaces with varying
access levels
– We know that,
– Public methods & variables are accessible to ALL
– Private methods & variables are only accessible to ‘friend’ classes
(C++)
– Package-Private methods & variables are only accessible to classes
in the same package (Java)
– Inner class has access to all methods & variables of its outer class
(Java)
– Narrow interface
– Declared ‘public’ and is accessible to ALL other classes (Same for
Java & C++)
– Wide interface
– Declared ‘private’ and accessible only to ‘friend’ classes ( C++)
or inner classes (Java)
– Declared ‘package-private’ and is accessible only to the classes in
the same package (Java)
Design Patterns 11
Consequences
• Advantages
– Maintains Encapsulation
• Memento provides a way to preserve the state of an object while
preserving encapsulation, in languages where this is possible
– data that only the Originator class should have access to effectively
remains private
– Simplifies the Originator
• preserves the simplicity of the Originator class by delegating the
saving and restoring of information to the Memento class
• Limitations
– Using Mementos might be Expensive
• amount of information that a Memento has to save might be quite
large, thus taking up fair amounts of storage
• Caretaker class (here the Mediator) may have to design strategies to
limit the number of objects for which it saves state
• if objects change in a predictable manner, we can only save the
incremental changes of an object’s state
Design Patterns 12