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 :)

Discussion

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.

Intent

  • 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.

Problem

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 :)

Use when:

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

Structure

The UML diagram of the memento pattern looks like this:

namespace MementoScheme
{
class Memento
{
private string state;
public Memento(string state)
{
this.state = state;
}
public string getState()
{
return state;
}
}
}
namespace MementoScheme
{
class Caretaker
{
private List<Memento> mementos = new List<Memento>();
public void addMemento(Memento m)
{
mementos.Add(m);
}
public Memento getMemento(int index)
{
return mementos[index];
}
}
}
namespace MementoScheme
{
class Originator
{
private string state;
public void setState(string state)
{
Console.WriteLine("Originator: Setting state to " + state);
this.state = state;
}
public Memento save()
{
Console.WriteLine("Originator: Saving to Memento.");
return new Memento(state);
}
public void restore(Memento m)
{
state = m.getState();
Console.WriteLine("Originator: State after restoring from Memento: " + state);
}
}
}
namespace MementoScheme
{
public class MementoDemo
{
public static void Main(String[] args)
{
Caretaker caretaker = new Caretaker();
Originator originator = new Originator();
originator.setState("State1");
originator.setState("State2");
caretaker.addMemento(originator.save());
originator.setState("State3");
caretaker.addMemento(originator.save());
originator.setState("State4");
originator.restore(caretaker.getMemento(1));
Console.ReadLine();
}
}
}

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.

namespace Mechanic
{
class Car
{
private string state;
public Car(string state)
{
this.state = state;
}
public string getState()
{
return state;
}
}
}
namespace Mechanic
{
class Caretaker
{
private List<Car> mementos = new List<Car>();
public void addMemento(Car m)
{
mementos.Add(m);
}
public Car getMemento(int index)
{
return mementos[index];
}
}
}
namespace Mechanic
{
class Mechanic
{
private string state;
public void setState(string state)
{
Console.WriteLine(state);
this.state = state;
}
public Car save()
{
return new Car(state);
}
public void restore(Car m)
{
state = m.getState();
Console.WriteLine("Returning to the previous work.");
Console.WriteLine(state);
}
}
}
namespace Mechanic
{
class Program
{
static void Main(string[] args)
{
Caretaker caretaker = new Caretaker();
Mechanic mechanic = new Mechanic();
mechanic.setState("The brake drum is in use. Begin of work");
caretaker.addMemento(mechanic.save());
mechanic.setState("The brake drum is available to use. End of work");
mechanic.restore(caretaker.getMemento(0));
Console.ReadLine();
}
}
}

Relations with other design patterns

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

Summary

That’s all about Memento🙂.

Entrepreneur and software developer