Memento
Memento as its name describes, which is for storing something. for recorving something from it.
We want to capture the type's inner state at a given time and also want to recove it to where it was captured but without jeopardizing the encapsulation without exposing the type's details to the client. The memento pattern makes it happen.
When we use this pattern
for storing the state first and recover the state from where it stored. We can do it with the normal method with a temp variable(instance) of the type and use the getter and setter to finish it. like below:
public class MyClient
{
public void StoreWithTheNormalWay()
{
MyType myType = new MyType();
myType.MyState = "First";
myType.Show();
MyType tempType = new MyType();
tempType.MyState = myType.MyState;
myType.MyState = "Changed";
myType.Show();
myType.MyState = tempType.MyState;
myType.Show();
}
}
public class MyType
{
private string myState;
public string MyState
{
get { return myState; }
set { myState = value; }
}
public void Show()
{
Console.WriteLine(myState);
}
}
The above code does make what we want with a temporary instance of MyType. and just reassign the state from the temporary to the myType. But you can find it was ugly with exposing the details of the MyType to the Client. what's worse? if the MyType changes it structure( and a field or remove a field. etc.) we got to update the client code. it is verbose and hard. How we resolve this. It is time for the memento pattern.
Actually the memento does use the temporary mechanism, but the memento encapsulate the details of the type to itself and let the Caretaker hold it. It is amazing.
Roles in this pattern
- Originator:responsible for creating the memento and recovering from the it
- Memento: the very memento to restore the state of the originator at a given time
- Caretaker:Responsible for hold the mementor but have no access to update the memento
A small demo to show what i have praised above:
namespace Memento
{
public class Memento
{
private string state;
public Memento(string state)
{
this.state = state;
}
public string State
{
get
{
return this.state;
}
}
}
public class Originator
{
private string state;
public string State
{
get { return this.state; }
set { this.state = value; }
}
public Memento CreateMemento()
{
Memento memento = new Memento(state);
return memento;
}
public void SetMemneto(Memento memento)
{
this.state = memento.State;
}
public void ShowState()
{
Console.WriteLine(this.state);
}
}
public class Caretaker
{
private Memento memento;
public Memento Memento
{
get { return this.memento; }
set { this.memento = value; }
}
}
class Program
{
static void Main(string[] args)
{
Originator originator = new Originator();
originator.State = "First";
originator.ShowState();//show "First"
Caretaker caretaker = new Caretaker();
caretaker.Memento = originator.CreateMemento();
originator.State = "Changed";
originator.ShowState();//show "Changed"
originator.SetMemneto(caretaker.Memento);
originator.ShowState();//show "First" AGAIN
}
}
}
public class MyClient
{
public void StoreWithTheNormalWay()
{
MyType myType = new MyType();
myType.MyState = "First";
myType.Show();
MyType tempType = new MyType();
tempType.MyState = myType.MyState;
myType.MyState = "Changed";
myType.Show();
myType.MyState = tempType.MyState;
myType.Show();
}
}
The newer version with the memento is better and more clarified. Let the originator store and recover by itself.and client even does not know how it works and also does not know the details of the originator which is one of the principles of the OOP.