The word 'memento' refers to an object that is kept as a reminder or souvenir of a person or event. Memento pattern is a behavioral pattern in GoF, as it captures and externalizes an object's internal state. This allows the object to be restored to its previous state at a later time. source: GoF overviewThe Memento is a class that represents a snapshot of an object's state and contains the state to be restored to an Originator. You can access the state encapsulated in a Memento by using its get and set methods. The Originator has a member function that creates and return a Memento with the current state of the Originator stored in the returned Memento. Originator also has a member function that sets its current state with the state of a given Memento object. In other words, it can create snapshot (Memento), or restore the state to the given snapshot (Memento). The Caretaker class is a helper class that is responsible for storing and restoring the state of one Originator through Memento objects. A Caretaker object holds and maintains Mementos, but never modifies them. The Caretaker class is responsible for the safekeeping of Mementos. For example, a Caretaker encapsulates a list of Mementos that can be added to or retrieved from the list. memento code exampleMemento class: public class Memento { Originator class: public class Originator { Caretaker class: public class Caretaker { main(): public class MementoPatternDemo { Output: Current State: State #4 when to use memento
comparisonMemento vs Command
Memento vs Temporal Collection
references
0 Comments
Leave a Reply. |
Categories
All
Archives
May 2020
|