Take the 2-minute tour ×

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
add comment

1 Answer

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... –  Baqueta Aug 6 '12 at 8:10
add comment

Your Answer


