I have been reading up on memento pattern from various sources of the internet. Differing information from different sources has left me in confusion regarding why this pattern is actually needed.

The dofactory implementation says that the primary intention of this pattern is to restore the state of the system.

Wiki says that the primary intention is to be able to restore the changes on the system. This gives a different impact - saying that it is possible for a system to have memento implementation with no need to restore. And that ability of restore is a feature of this.

OODesign says that

It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure.

So, my question is why exactly do we use this one? Is it to save previous states - or to promote encapsulation between the Caretaker and the Memento? Why is this type of encapsulation so important?

Edit: For those visiting, check out this Implementation!

Edit: I am working in implementing a memento solution to my problem. I will post another question regarding that and link that question to this one. Thanks all for responding with valuable suggestions!

Edit 3: Here is the link to my sample implementation


3 Answers 3


A similar pattern, Memo, or Memoization, stores state as well, but it is often used as a program-speed optimization. If a time-consuming operation has a limited number of common inputs and outputs, the most common ones (or all of them) can be stored in a hashtable. When called again with the same inputs, it first checks the hashtable and if it finds them, it returns the previous output without recalculating it.

I imagine a Memento pattern could be used for performance as well - instead of doing all the reverse-calculations for a reverse state change, just restore from the previous state. Some functions are one-way, so there is no undo unless you store the previous state.

You could use a Memento pattern to memoize a periodic, or symmetric function like sine. Calculate all the values from 0-180 degrees, then go backwards through them to get the negative values from 180-360. Better yet, calculate values from 0-90, then go backwards through those values to get 90-180, forwards for 180-270, and backwards from 270-360.

Ctrl-Z in Word, or the undo function of any software is likely implemented by using the memento pattern, or in some cases, possibly by a reverse of the function that made each change. In the second case, the history of which functions were called would be mementos, so I suppose the memento pattern is always used for undo.

  • memorization pattern used to avoid reverse calculation and for periodic functions - thanks for this info! Nothing like a practical example to get the concepts to seep in! Aug 28, 2012 at 2:03
  • It can also be used with something like Newton's method for square root approximation to decide when you are done. mitpress.mit.edu/sicp/full-text/book/… Since the underlying floating-point representation has limited accuracy, all numbers will eventually have repeating approximations. If you know what the old approximations were, you can stop approximating the first time you repeat an answer and get maximal accuracy (at the expense of some time and memory). Sep 24, 2012 at 15:46

By itself, the Memento Pattern is just used to capture and save states. The encapsulation exists only to protect the states from the rest of the system - once a state is captured, it needs to be controlled carefully. For example, it doesn't make sense to be able to change a previous state once it's been saved (that would be changing history) and it might not make sense to simply return to a previous state of one object without affecting others (to prevent the system from entering an entirely invalid state).

The most common use of a Memento that I've seen is to support undo functionality. It is also related to storing redo functionality by moving back forward in time.

  • I've also used it in copy logic and serialization logic (to send objects over a wire, or save state to a file). Aug 27, 2012 at 13:47
  • @ScottWhitlock Saving state to a file makes sense for Memento. The Memento object would have the ability to write itself to a specified format and create a new one by reading this format, while the Caretaker would provide the interface to save state to an external format or load state from the external format - it's just an extension to saving state in memory. I can't really see it's use in copy logic, though. I'd have to think about it a lot more.
    – Thomas Owens
    Aug 27, 2012 at 13:50
  • 1
    Here is an example of using it in copy logic in VB6. Aug 27, 2012 at 14:28
  • @ThomasOwens, thanks for that important part in your explanation that the memento is basically to "save" states. It is the implementer's discretion to restore or not! I found this part of the explanation missing from all my references!! Aug 28, 2012 at 1:58
  • @ScottWhitlock, thanks much for the link to your memento implementation! I was able to understand not only its basic use, but also intuitive use like you have done! Aug 28, 2012 at 1:59

All these definitions steer you in the same direction; they are all saying the point is to be able to restore something to its previous state. That something can be the entire system, or just a single object.

This pattern is useful if a record changes state over time, but you have the business requirement to be able to restore it to any previous state at any time. Or alternatively, if you need to be able to view the record as it was at any previous point time. These types of requirements are very common in many types of systems.

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.