0

In GOF Book, it says the structure of the Memento design pattern is like this: GOF Memento Pattern
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?

subject-q
  • 91
  • 3
  • 19
xun yan
  • 69
  • 5
  • 3
    Both are correct ... in the sense that neither is saying things that are wrong about how memento could be implemented. It is a matter of opinion which is "more correct". We don't "do" matter of opinion questions on StackOverflow. – Stephen C Jul 16 '19 at 02:35
  • 1
    Those show only trivial differences, particularly using a C++-type pseudocode style vs. Java style. – chrylis -cautiouslyoptimistic- Jul 16 '19 at 02:49
  • I can't see that these are equivalent, contrary to my esteemed colleagues, as the flow of control and visibility are 100% opposite, but don't stress about Design Patterns, they were just a passing fancy. Fewer than half of those in the GoF book are in widespread use. – user207421 Jul 16 '19 at 03:29
  • 1
    I would ignore Wikipedia when it comes to design patterns. For an online resource, try https://sourcemaking.com/design_patterns/memento. – jaco0646 Jul 16 '19 at 23:44

2 Answers2

1

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.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
1

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.