SlideShare une entreprise Scribd logo
1  sur  19
-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

Contenu connexe

Tendances

Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 
Pertemuan 11 thread dan asyntask
Pertemuan 11 thread dan asyntaskPertemuan 11 thread dan asyntask
Pertemuan 11 thread dan asyntask
heriakj
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain
 

Tendances (20)

SAD11 - Sequence Diagrams
SAD11 - Sequence DiagramsSAD11 - Sequence Diagrams
SAD11 - Sequence Diagrams
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Android Lesson 3 - Intent
Android Lesson 3 - IntentAndroid Lesson 3 - Intent
Android Lesson 3 - Intent
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Java awt
Java awtJava awt
Java awt
 
Asp.net html server control
Asp.net html  server controlAsp.net html  server control
Asp.net html server control
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Pertemuan 11 thread dan asyntask
Pertemuan 11 thread dan asyntaskPertemuan 11 thread dan asyntask
Pertemuan 11 thread dan asyntask
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 

En vedette (9)

Mediator Design Pattern
Mediator Design PatternMediator Design Pattern
Mediator Design Pattern
 
Mediator Pattern
Mediator PatternMediator Pattern
Mediator Pattern
 
Memento
MementoMemento
Memento
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Observer Pattern
Observer PatternObserver Pattern
Observer Pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Deploying and releasing applications
Deploying and releasing applicationsDeploying and releasing applications
Deploying and releasing applications
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Mediator
MediatorMediator
Mediator
 

Plus de Sayanton Vhaduri

Plus de Sayanton Vhaduri (7)

CSS_Dibbo
CSS_DibboCSS_Dibbo
CSS_Dibbo
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Result_Processing
Result_ProcessingResult_Processing
Result_Processing
 
Cell Phone Operated Land Rover
Cell Phone Operated Land RoverCell Phone Operated Land Rover
Cell Phone Operated Land Rover
 
Power Sector In Bangladesh
Power Sector In BangladeshPower Sector In Bangladesh
Power Sector In Bangladesh
 
Place_Identifier_App
Place_Identifier_AppPlace_Identifier_App
Place_Identifier_App
 
Diabetic_Monitor
Diabetic_MonitorDiabetic_Monitor
Diabetic_Monitor
 

Dernier

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Memento pattern

  • 1. -Sayanton Vhaduri Dibbo Department of 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, May 13, 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, 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
  • 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, 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.
  • 18. 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.
  • 19. Friday, May 13, 201619