SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design PatternAsif Tayef
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern PresentationJAINIK PATEL
 
Command Design Pattern
Command Design Pattern Command Design Pattern
Command Design Pattern anil kanzariya
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Androidma-polimi
 
Proxy Design Patterns
Proxy Design PatternsProxy Design Patterns
Proxy Design PatternsZafer Genc
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton PatternMudasir Qazi
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Patternsahilrk911
 
Behavioral Design Patterns
Behavioral Design PatternsBehavioral Design Patterns
Behavioral Design PatternsLidan Hifi
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksMaulik Shah
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
 
Constructor in Java - ITVoyagers
Constructor in Java - ITVoyagersConstructor in Java - ITVoyagers
Constructor in Java - ITVoyagersITVoyagers
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Shared preferences
Shared preferencesShared preferences
Shared preferencesSourabh Sahu
 

What's hot (20)

Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Command Design Pattern
Command Design Pattern Command Design Pattern
Command Design Pattern
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Proxy Design Patterns
Proxy Design PatternsProxy Design Patterns
Proxy Design Patterns
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Behavioral Design Patterns
Behavioral Design PatternsBehavioral Design Patterns
Behavioral Design Patterns
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
05 intent
05 intent05 intent
05 intent
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Constructor in Java - ITVoyagers
Constructor in Java - ITVoyagersConstructor in Java - ITVoyagers
Constructor in Java - ITVoyagers
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Notification android
Notification androidNotification android
Notification android
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
 

Viewers also liked

Viewers also liked (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
 

More from Sayanton Vhaduri

More from 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
 

Recently uploaded

Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 

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