[review]Design Pattern:Memento

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.




转载于:https://www.cnblogs.com/sanjia/archive/2011/12/01/2271283.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值