What is Memento Design Pattern

Why do we use patterns?

They require us a way to solve problems related to software development with a proven solution.

We will start to talk about design patterns in programming and our first pattern is going to be Memento Pattern.The main aim of the Memento pattern is to save the state of an object and the restore it again if necessary.

If you are asking what is what is state of object? Check this link out before I write whole another paper: https://marcus-biel.com/what-is-an-objects-state/

We can easily define it as a UNDO action like in MS Word or like used in some games which have been saved before, long in short you can use memento pattern when you want to restore your state of object to previous state of the object.

So, how we will do it?

We are gonna have three special classes in this pattern which are they called originator, memento and caretaker.

What originator class do ?What do you think?

Originator class; is going to be our “saver and restorer” of state object.

Caretaker class; will store all states of the object on which it operates and returns these states and adds new states of the object.

Memento class; will store the current state of the object, its state can be read by the Originator class.

So after all technical and theorical part we need some codes and we have to finish homework actually:) We are gonna write code for undo our move.

Let’s start with the Memento Class!

It stores and returns the current state of the object.

Let’s go on with our caretaker class.

Here is our originator class…

And we can’t forget our Main class never ever of course.

I hope it was explaining enough you to understand the basic structure.

I want to keep update with other main design patterns as well.

See you in next pattern.

Byee!

Computer Science Student/Poland