The word 'memento' refers to an object kept as a reminder or souvenir of a person or event. Memento pattern is a behavioral pattern in GoF because it captures and externalize an object's internal state so that the object can be restored to this state at a later time. ![]() source: GoF overviewMemento, the class that represents a snapshot, simply contains the state of an object to be restored to an Originator. You and get and set state that a Memento encapsulates inside. Originator, the class in which the current state is held. 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). Caretaker is the helper class that is responsible for storing and restoring the Originator’s state through Memento object. A Caretaker object keeps and holds the Mementos, but never modify the Mementos. Caretaker class is responsible for the memento's safekeeping. For example, a Caretaker encapsulates a list of Mementos that you can add more to the list, or get a Memento 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
references
0 Comments
Leave a Reply. |
Categories
All
Archives
May 2020
|