Memento Design Pattern in Modern C++ is a very straight forward Behavioural Design Pattern. The motivation behind using the Memento Design Pattern is to keep some sort of token which then allows you to restore an object to a particular state. This is particularly useful if you have a system with medieval components i.e. an object or indeed a set of objects goes through a set of changes.
By the way, If you haven’t check out my other articles on Behavioural Design Patterns, then here is the list:
- Chain of responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords like override
, final
, public
(while inheritance) just to make code compact & consumable(most of the time) in single standard screen size. I also prefer struct
instead of class
just to save line by not writing “public:
” sometimes and also miss virtual destructor, constructor, copy constructor, prefix std::
, deleting dynamic memory, intentionally. I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.
Note:
- If you stumbled here directly, then I would suggest you go through What is design pattern? first, even if it is trivial. I believe it will encourage you to explore more on this topic.
- All of this code you encounter in this series of articles are compiled using C++20(though I have used Modern C++ features up to C++17 in most cases). So if you don’t have access to the latest compiler you can use https://wandbox.org/ which has preinstalled boost library as well.
Intent
To store and restore the state of the component/object.
- In keep changing & well-designed OOPs software systems, the usual problem you may face while implementing rollback functionality is encapsulation. Because the object’s representation (data structure) is hidden. And can’t access from outside the object directly without using setter & getter.
- Memento Design Pattern is the right way to address this problem. Memento is a kind of immutable object. It captures & externalizes an object’s internal state at given a particular time without violating encapsulation. So that the object can restore to that state on later point of time.
Memento Design Pattern Example in C++
- With continuing our previous example of the bank account from Command Design Pattern where we were recording every change as a command & made facility to undo that command using member function.
- Here in Memento Design Pattern, we simply save the snapshot of the system/component at a particular point of time. And allow the user to roll back the system to that snapshot.
|
|
- So as you can see the state of the system is sufficiently small in terms of the memory footprint to actually record every single change and as a result not only do you get the user to be able to restore the system to any particular state just by using the memento.
- But you also have this ability to walk forwards and backwards in terms of the overall timeline. You let the user kind of undo and redo depending on their needs.
- So this is the proper way by which you can implement memento to jump back from one state to another. Undo mechanism is slightly different from we have seen earlier in the Command Design Pattern.
Benefits of Memento Design Pattern
- Because what we’re doing here is Undo-ing at a discrete point of time unlike a line of changes we looked at the Command Design Pattern. By using memento, you can go backwards and forwards or you can go to discrete points of time that you’ve saved by saving a memento of that point in time & restore it.
- A memento is very useful in almost all applications which must restart from their last known working state or draft. An example of this can be an IDE which restarts from changes user-made before closing the IDE.
- Memento Design Pattern maintains high cohesion.
Summary by FAQs
Difference between Command & Memento Design Pattern?
- In Command Design Pattern, the token represents a request; in Memento, it represents the internal state of an object at a particular time.
- Polymorphism is important to Command Design Pattern, but not to Memento because its interface is so narrow that a memento can only be passed as a value.
Difference between State & Memento Design Pattern?
- State Design Pattern is used to dictates the previous, current or future behaviour of the system.
- While Memento Design Pattern is typically used to store only the historical state of an object which also does not have any direct relation to behaviour.