Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
up vote 10 down vote accepted

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.

share|improve this answer
Thanks, makes sense! – Jonas H. Aug 5 '12 at 19:26
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. – Jarrod Roberson 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 posting your answer, you agree to the privacy policy and terms of service.

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