Design patterns: Memento

We are starting another design pattern today, it is called Memento, it is a very simple to understand pattern, also the main picture of this article also translates the pattern, at the same time very attracting attention 🙂 mainly it is mean that you can restore the state of the object to the previous one, the pattern is more precisely explain further in the article 🙂



The main purpose of the Memento pattern is to save the internal state of an object and the possibility of restoring it again if necessary, without disturbing encapsulation.

The implementation of this pattern consists mainly of three classes:

Originator – a class that saves and restores the state of the object.

Caretaker – a class that stores all states of the object on which it operates and returns these states and adds new states of the object.

Memento- class that stores the current state of the object, its state can be read by the Originator class.

A great example of using the Memento pattern is, for example, restoring previous versions of plugins in wordpress if you used wordpress and you had the WP Rollback plugin installed, it was a new button next to the plugin name the Rollback that is used to restore the plugin to the previous version, as shown in the picture below.

The creators of this plugin used the Memento pattern for 100% 🙂 Memento is also used in games, when the player wants to load the previous state of the previously saved game.



  • Saving the internal state of the object, and if it necessary, restoring the previously saved state of the object.
  • Non-violation of encapsulation of the object on which we operate.



The problem in which you use the memento pattern is very simple 🙂 you want to do a function to save and load the state of the game or make a mechanism to restore your program, which we assume you are now creating to the previous version, in this case it will be best to use memento 🙂

Those mature ones know what’s going on in the picture above 😂😂


Use when:

  • You need to restore the state of the object to its previous state.



The UML diagram of the memento pattern looks like this:

The explanation of the operation of these classes is already in the Discussion section at the top of the article, so I probably do not have to repeat myself 🙂

I know you feel very smart 🙂

However, it’s time to go to the code 🙂 For real work 🙂

Let’s start with the Memento class.

As you can see, this class, as it was explained earlier, stores and returns the current state of the object.

Caretaker class.

And Originator class.

As it has been said before, this class, as you can see, has methods responsible for saving and restoring the state of the object. In the save() method, we need to get the current state of the object, so we must use the Memento class the same in the restore() method.

Once again I repeat the explanation of the function of all these classes is in the Discussion section at the top of the article 🙂

Of course, the customer was left who uses these classes.



Real-life example

Replacing the brake drum

Suppose we have to replace the brake drum in the car and it has to do a robot for us.

It requires to undermine the car and to remove the wheel then change the brake drum and return to the previous state to put the wheel and normally put the car 🙂 In the robot that has to do it for us it is will be well to upload software using the pattern Memento 🙂 For a better understanding is the picture below.

And again to the code 🙂 Let’s do it in the order as in the previous example 🙂 In total, I will not need the code even seems to explain to me because it is practically the same as in the previous example only the names are different 🙂

So we start from the Memento class in our case it’s called Car.

You can see that the same as previously only the class name is different 🙂

Caretaker class.

Same as previously only also names are different as well 🙂

The Originator class in our case is called Mechanic.

The logic of this class is the same as in the previous example 🙂

And client 🙂



Relations with other design patterns

  • Memento is sometimes used in conjunction with the iterator to control the state of iteration



That’s all about Memento🙂.

Link to github with the whole code from this article:

This content also you can find on my steemit blog:

And on medium:

In the next article, we will talk about the Command pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb: Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

Illustrations, pictures and diagrams are from:

If that post was useful for you share it with your friends :)

Post a comment

Be first!

Notify about