-Sayanton Vhaduri Dibbo
Department of Computer Science &
Engineering
University of Dhaka, Bangladesh
Memento Pattern
or Token
Topics
Friday, May 13, 20162
 Intent
 Motivation
 Applicability
 Participants
 Example
 Real life scenerio
 Coding
 Class Diagram
 Advantage & Disadvantage
Intent
Friday, May 13, 20163
 A Behavioral Pattern that can be used to promote
undo or rollback to object status.
• Without violating encapsulation, capture and
externalize an object’s internal state so that the
object can be returned to this state later
Motivation
Friday, May 13, 20164
 Often it is needed to backtrack when a particular path
proves unproductive.
 Examples: graph algorithms, text navigation etc.
 The exploration of some complex data structure is
done by many technical processes.
Applicability
Friday, May 13, 20165
To Keep a
snapshot of
object to
restore later
When using
direct interface to
restore state will
violate design
rule
Participants
Friday, May 13, 20166
 Memento
o stores internal state of the Originator object
o Inner class of originator & private.
o The Memento must have two interfaces
 Originator
o Creates a memento object
o Use the memento to save & restore
 Caretaker
o Responsible for keeping the memento
o The memento is black box to the caretaker,
and the caretaker must not operate on it
Internal behavior of code is unknown
Example
Friday, May 13, 20167
Memento
Caretaker
Originator
Stores internal
materials of the house
(like the store room of
home)
Responsible for
storing &
maintaining
household good
(like a night guard)
Who stores in
house(like the
owner of the
house)
Use Of
Memento
Friday, May 13, 20168
 Games
Database
Transaction
Mom
Alice
Well. I want
to add…….
• Editor (any
type of
files)
 Calculator
Real life Scenerio
Friday, May 13, 20169
I’m getting fatty!
Which pattern can
restore me to my
slim previous
state???
Solution
Friday, May 13, 201610
Yes!
Memento
Is the
solution
*Diet_info class: originator
i. Dieter field
ii. No of diet day
iii. Weight of dieter
*inside memento class to save
previous state.
*save(): creates & return object
*Restore(): return previous state
Code
Friday, May 13, 201611
public class DietInfo {
String personName;
int dayNumber;
int weight;
public DietInfo(String personName, int dayNumber, int weight) {
this.personName = personName;
this.dayNumber = dayNumber;
this.weight = weight;
}
public String toString() {
return "Name: " + personName + ", day number: " + dayNumber + ",
weight: " + weight;
}
public void setDayNumberAndWeight(int dayNumber, int weight) {
this.dayNumber = dayNumber;
this.weight = weight;
}
Originator
class
Code
Friday, May 13, 201612
public Memento save() {
return new Memento(personName, dayNumber, weight);
}
public void restore(Object objMemento) {
Memento memento = (Memento) objMemento;
personName = memento.mementoPersonName;
dayNumber = memento.mementoDayNumber;
weight = memento.mementoWeight;
}
private class Memento {
String mementoPersonName;
int mementoDayNumber;
int mementoWeight;
public Memento(String personName, int dayNumber, int weight) {
mementoPersonName = personName;
mementoDayNumber = dayNumber;
mementoWeight = weight;
}
}
creates
and
returns a
Memento
object
restore the
state of
the Diet
Info
save the
state of
DietInfo
Code
Friday, May 13, 201613
// note that DietInfo.Memento isn't visible to the caretaker so we need to cast the
memento to Object//
public class DietInfoCaretaker {
Object objMemento;
public void saveState(DietInfo dietInfo) {
objMemento = dietInfo.save();
}
public void restoreState(DietInfo dietInfo) {
dietInfo.restore(objMemento);
}
}
caretaker
class
stores the
state
Of dietinfo
class
Code
Friday, May 13, 201614
public class MementoDemo {
public static void main(String[] args) {
// caretaker
DietInfoCaretaker dietInfoCaretaker = new DietInfoCaretaker();
// originator
DietInfo dietInfo = new DietInfo("Fred", 1, 100);
System.out.println(dietInfo);
dietInfo.setDayNumberAndWeight(2, 99);
System.out.println(dietInfo);
System.out.println("Saving state.");
dietInfoCaretaker.saveState(dietInfo);
dietInfo.setDayNumberAndWeight(3, 98);
System.out.println(dietInfo);
dietInfo.setDayNumberAndWeight(4, 97);
System.out.println(dietInfo);
System.out.println("Restoring saved state.");
dietInfoCaretaker.restoreState(dietInfo);
System.out.println(dietInfo);
Class Diagram
Friday, May 13, 201615
Diet info(Originator)
+Person name:string
+day no:int
+weight: int
+DietInfo():void
+setDayNumberAndWeight()
:void
+save() :string
+Restore() :void
- Memento() :void
Memento
-mementoPersonName: string
-mementoDayNumber: int
-mementoWeight :int
+memento(): void
Diet info
caretaker(caretak
er)
+Object objMemento
+saveState() :void
+restoreState :void
Creates a
memento
object
stores
internal state
Object stored
by caretaker
Advantages & disadvantages
Friday, May 13, 201616
 • Benefits:
 Provides a way of recording the internal state of an object
in a separate object without violating law of design
pattern.
• Eliminates the need for multiple creation of the same
object for the sole purpose of saving its state.
• Simplifies the Originator by giving responsibility of
Memento storage among the Caretakers.
Advantages &
disadvantages(cont…)
Friday, May 13, 201617
 • Drawbacks:
 Saving and restoring state may be time consuming.
 Memento object must provide two types of interfaces: a
narrow interface for Caretaker and a wide interface for the
Originator.
• enables the other object to arbitrarily change the state of
object.
Related Patterns
Friday, May 13, 201618
• Command :
o Commands can use mementos to maintain state for
undoable operations.
o But usually command pattern does not provide this undo
operation.
• Iterator :
o Mementos can be used for iteration to keep the state of
every elements saved in.
Friday, May 13, 201619

Memento pattern

  • 1.
    -Sayanton Vhaduri Dibbo Departmentof Computer Science & Engineering University of Dhaka, Bangladesh Memento Pattern or Token
  • 2.
    Topics Friday, May 13,20162  Intent  Motivation  Applicability  Participants  Example  Real life scenerio  Coding  Class Diagram  Advantage & Disadvantage
  • 3.
    Intent Friday, May 13,20163  A Behavioral Pattern that can be used to promote undo or rollback to object status. • Without violating encapsulation, capture and externalize an object’s internal state so that the object can be returned to this state later
  • 4.
    Motivation Friday, May 13,20164  Often it is needed to backtrack when a particular path proves unproductive.  Examples: graph algorithms, text navigation etc.  The exploration of some complex data structure is done by many technical processes.
  • 5.
    Applicability Friday, May 13,20165 To Keep a snapshot of object to restore later When using direct interface to restore state will violate design rule
  • 6.
    Participants Friday, May 13,20166  Memento o stores internal state of the Originator object o Inner class of originator & private. o The Memento must have two interfaces  Originator o Creates a memento object o Use the memento to save & restore  Caretaker o Responsible for keeping the memento o The memento is black box to the caretaker, and the caretaker must not operate on it Internal behavior of code is unknown
  • 7.
    Example Friday, May 13,20167 Memento Caretaker Originator Stores internal materials of the house (like the store room of home) Responsible for storing & maintaining household good (like a night guard) Who stores in house(like the owner of the house)
  • 8.
    Use Of Memento Friday, May13, 20168  Games Database Transaction Mom Alice Well. I want to add……. • Editor (any type of files)  Calculator
  • 9.
    Real life Scenerio Friday,May 13, 20169 I’m getting fatty! Which pattern can restore me to my slim previous state???
  • 10.
    Solution Friday, May 13,201610 Yes! Memento Is the solution *Diet_info class: originator i. Dieter field ii. No of diet day iii. Weight of dieter *inside memento class to save previous state. *save(): creates & return object *Restore(): return previous state
  • 11.
    Code Friday, May 13,201611 public class DietInfo { String personName; int dayNumber; int weight; public DietInfo(String personName, int dayNumber, int weight) { this.personName = personName; this.dayNumber = dayNumber; this.weight = weight; } public String toString() { return "Name: " + personName + ", day number: " + dayNumber + ", weight: " + weight; } public void setDayNumberAndWeight(int dayNumber, int weight) { this.dayNumber = dayNumber; this.weight = weight; } Originator class
  • 12.
    Code Friday, May 13,201612 public Memento save() { return new Memento(personName, dayNumber, weight); } public void restore(Object objMemento) { Memento memento = (Memento) objMemento; personName = memento.mementoPersonName; dayNumber = memento.mementoDayNumber; weight = memento.mementoWeight; } private class Memento { String mementoPersonName; int mementoDayNumber; int mementoWeight; public Memento(String personName, int dayNumber, int weight) { mementoPersonName = personName; mementoDayNumber = dayNumber; mementoWeight = weight; } } creates and returns a Memento object restore the state of the Diet Info save the state of DietInfo
  • 13.
    Code Friday, May 13,201613 // note that DietInfo.Memento isn't visible to the caretaker so we need to cast the memento to Object// public class DietInfoCaretaker { Object objMemento; public void saveState(DietInfo dietInfo) { objMemento = dietInfo.save(); } public void restoreState(DietInfo dietInfo) { dietInfo.restore(objMemento); } } caretaker class stores the state Of dietinfo class
  • 14.
    Code Friday, May 13,201614 public class MementoDemo { public static void main(String[] args) { // caretaker DietInfoCaretaker dietInfoCaretaker = new DietInfoCaretaker(); // originator DietInfo dietInfo = new DietInfo("Fred", 1, 100); System.out.println(dietInfo); dietInfo.setDayNumberAndWeight(2, 99); System.out.println(dietInfo); System.out.println("Saving state."); dietInfoCaretaker.saveState(dietInfo); dietInfo.setDayNumberAndWeight(3, 98); System.out.println(dietInfo); dietInfo.setDayNumberAndWeight(4, 97); System.out.println(dietInfo); System.out.println("Restoring saved state."); dietInfoCaretaker.restoreState(dietInfo); System.out.println(dietInfo);
  • 15.
    Class Diagram Friday, May13, 201615 Diet info(Originator) +Person name:string +day no:int +weight: int +DietInfo():void +setDayNumberAndWeight() :void +save() :string +Restore() :void - Memento() :void Memento -mementoPersonName: string -mementoDayNumber: int -mementoWeight :int +memento(): void Diet info caretaker(caretak er) +Object objMemento +saveState() :void +restoreState :void Creates a memento object stores internal state Object stored by caretaker
  • 16.
    Advantages & disadvantages Friday,May 13, 201616  • Benefits:  Provides a way of recording the internal state of an object in a separate object without violating law of design pattern. • Eliminates the need for multiple creation of the same object for the sole purpose of saving its state. • Simplifies the Originator by giving responsibility of Memento storage among the Caretakers.
  • 17.
    Advantages & disadvantages(cont…) Friday, May13, 201617  • Drawbacks:  Saving and restoring state may be time consuming.  Memento object must provide two types of interfaces: a narrow interface for Caretaker and a wide interface for the Originator. • enables the other object to arbitrarily change the state of object.
  • 18.
    Related Patterns Friday, May13, 201618 • Command : o Commands can use mementos to maintain state for undoable operations. o But usually command pattern does not provide this undo operation. • Iterator : o Mementos can be used for iteration to keep the state of every elements saved in.
  • 19.