Search code examples
javadesign-patternsmemento

Which among these is the proper implementation of the Memento design pattern?


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?


Solution

  • 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.