memento pattern
TRANSCRIPT
Memento Pattern or Token
-Sayanton Vhaduri DibboDepartment of Computer Science & EngineeringUniversity of Dhaka, BangladeshMemento Patternor Token
TopicsFriday, May 13, 20162IntentMotivationApplicabilityParticipantsExampleReal life scenerioCodingClass DiagramAdvantage & Disadvantage
IntentFriday, May 13, 2016
3
A Behavioral Pattern that can be used to promote undo or rollback to objectstatus.
Without violating encapsulation, capture and externalize an objects internal state so that the object can be returned to this statelater
MotivationFriday, 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.
ApplicabilityFriday, May 13, 20165
To Keep a snapshot of object to restore laterWhen using direct interface to restore state will violate design rule
ParticipantsFriday, May 13, 20166Mementostores internal state of the Originator objectInner class of originator & private.The Memento must have two interfaces
OriginatorCreates a memento objectUse the memento to save & restore
CaretakerResponsible for keeping the mementoThe memento is black box to the caretaker, and the caretaker must not operate on it
Internal behavior of code is unknown
ExampleFriday, May 13, 20167
MementoCaretakerOriginatorStores 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 MementoFriday, May 13, 20168
GamesDatabase Transaction
Mom Alice
Well. I want to add.Editor (any type of files)Calculator
Real life ScenerioFriday, May 13, 20169
Im getting fatty!Which pattern can restore me to my slim previous state???
SolutionFriday, May 13, 201610
Yes!MementoIs the solution*Diet_info class: originatorDieter fieldNo of diet dayWeight of dieter*inside memento class to save previous state.*save(): creates & return object*Restore(): return previous state
Code Friday, May 13, 201611public 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
CodeFriday, May 13, 201612public 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 objectrestore the state of the Diet Infosave the state of DietInfo
CodeFriday, 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 classstores the stateOf dietinfo class
CodeFriday, May 13, 201614public class MementoDemo {
public static void main(String[] args) { // caretakerDietInfoCaretaker dietInfoCaretaker = new DietInfoCaretaker();
// originatorDietInfo 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 DiagramFriday, 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(): voidDiet info caretaker(caretaker)
+Object objMemento
+saveState() :void+restoreState :void
Creates a memento objectstores internal state Object stored by caretaker
Advantages & disadvantagesFriday, 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 PatternsFriday, May 13, 201618Command :
Commands can use mementos to maintain state for undoable operations.
But usually command pattern does not provide this undo operation.
Iterator :
Mementos can be used for iteration to keep the state of every elements saved in.
Friday, May 13, 201619
Thank You!