In GOF Book, it says the structure of the Memento design pattern is like this:
But when I searched Wikipedia, it says the structure of the Memento design pattern is like this:
Which among the both is the correct implementation?
In GOF Book, it says the structure of the Memento design pattern is like this:
But when I searched Wikipedia, it says the structure of the Memento design pattern is like this:
Which among the both is the correct implementation?
All of those diagrams show different aspects of the same momento pattern implementation.
Both of the class diagrams are incomplete:
The GOF diagram is missing the association between the caretaker and originator. Most class diagrams leave out a lot of associations, but this one is important and should really be in there.
The Wikipedia diagram leaves out the fact that the caretaker holds on to the momento. That's also an important part of the pattern that should really be in the diagram.
GOF also correctly notes that the Originator -> Momento arrow is a dependency, not just an association, because the Originator class requires the functionality of the Momento class to implement its interface.
The sequence diagram is the most useful in explaining how it works.
Both of these diagrams explain the same concept but in different ways. The key concept of Momento pattern is that Momento pattern is used to save the state of the object and to restore state afterwards. It consists of four classes basically:
1. Originator Class: The originator class creates and stores the state in objects of Memento 2. Momento Class: Momento class restores the object state from Momento 3. CareTaker Class: The caretaker class holds the list of Momento objects 4. MomentoDemo Class: It uses both Originator and Caretaker classes and uses its objects to show the restoration of the states of objects.