In the Gang of Four's "Memento" design pattern, the Memento class looks like a useless wrapper that doesn't actually provide any value to me.

What is it for? Is there any reason you can't just pass around the String object (in the Wikipedia example)?


The memento class is the one holding the different states you are dealing with in your object - it is what enables the "undo/redo" functionality.

The example you have linked to on Wikipedia is rather simplistic, where the "state" is just a string value. Consider that the state could be a bunch of properties that you may want to restore in "batch" and perhaps have several "undo" points with.

  • 4
    Also note that the use of .clone() in that wikipedia example is extremely bad practice. It will never work like you want it to, especially in this pattern. Whomever wrote that example needs their keyboard taken away from them. – user7519 Aug 6 '12 at 2:39
  • The memento object in the 'vanilla' memento pattern is purely used to enable "undo". The wikipedia example (which really isn't that bad IMO) seems to bear this out... – vaughandroid Aug 6 '12 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.