0% found this document useful (0 votes)
29 views12 pages

Memento

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 12

Design Patterns

Memento Design Pattern

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

• This pattern defines three roles for objects:


– Originator
• object whose state we want to save
– Memento
• another object that saves the state of the Originator
– Caretaker
• manages the timing of the saving of the state
• saves the Memento and
• if needed, uses the Memento to restore the state of the Originator
*** never operates on or examines the contents of a Memento ***
Design Patterns 5
Structure: Class Diagram
Instantiation Relation – Originator instantiates the Memento

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:

• Sometimes the caretaker won't pass the memento back to the


originator, because the originator might never need to revert to an
earlier state
• Mementos are passive: Only the originator that created a
memento will assign or retrieve its state
Design Patterns 7
Implementation Issues
• Simplest Method: Make all the member variables ‘public’
– Not a good approach !
 Saving state of an object without making all of its variables
public
– can be done with varying degrees of success in various languages

• C++: ‘friend’ keyword


– Gives a non-member function access to the private members of a class
– Friend function is not a member of the class, but has the privileges of a
member function in the class in which it is declared
– Must be declared inside the class declaration to which it is a friend
– If ALL member functions of a class A are friend functions of a second
class B, we can declare the complete class A as a friend in class B

• 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

You might also like