C# 备忘录模式(Memento)
获取目标对象的当前内部状态,并将状态保存在对象外部,以后可以从将目标对象恢复到保存时的状态。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DesignMode.Memento
{
public class MyAim
{
private string _state1;
private int _state2;
private decimal _state3;
public decimal State3
{
get { return _state3; }
set { _state3 = value; }
}
public int State2
{
get { return _state2; }
set { _state2 = value; }
}
public string State1
{
get { return _state1; }
set { _state1 = value; }
}
public MyAim()
{
this._state1 = "1";
this._state2 = 1;
this._state3 = 1.0M;
}
public void SetState()
{
this._state1 = "2";
this._state2 = 2;
this._state3 = 2.0M;
}
public void DisplayState()
{
MessageBox.Show(" state1:" + State1 + "\n state2:" + State2 + "\n state3:" + State3);
}
//保存当前状态
public MyMemento SaveState()
{
return new MyMemento(_state1, _state2, _state3);
}
//恢复状态
public void RecoveryState(MyMemento myMemento)
{
this._state1 = myMemento.State1;
this._state2 = myMemento.State2;
this._state3 = myMemento.State3;
}
}
//备忘录类,用于保存当前状态
public class MyMemento
{
private string _state1;
private int _state2;
private decimal _state3;
public MyMemento(string state1,int state2,decimal state3)
{
this._state1 = state1;
this._state2 = state2;
this._state3 = state3;
}
public MyMemento()
{
}
public decimal State3
{
get { return _state3; }
set { _state3 = value; }
}
public int State2
{
get { return _state2; }
set { _state2 = value; }
}
public string State1
{
get { return _state1; }
set { _state1 = value; }
}
}
//将备忘录对象保存到外部类
public class ExternalSave
{
private MyMemento myMemento;
public MyMemento MyMemento
{
get { return myMemento; }
set { myMemento = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DesignMode.Memento
{
public class MyAim
{
private string _state1;
private int _state2;
private decimal _state3;
public decimal State3
{
get { return _state3; }
set { _state3 = value; }
}
public int State2
{
get { return _state2; }
set { _state2 = value; }
}
public string State1
{
get { return _state1; }
set { _state1 = value; }
}
public MyAim()
{
this._state1 = "1";
this._state2 = 1;
this._state3 = 1.0M;
}
public void SetState()
{
this._state1 = "2";
this._state2 = 2;
this._state3 = 2.0M;
}
public void DisplayState()
{
MessageBox.Show(" state1:" + State1 + "\n state2:" + State2 + "\n state3:" + State3);
}
//保存当前状态
public MyMemento SaveState()
{
return new MyMemento(_state1, _state2, _state3);
}
//恢复状态
public void RecoveryState(MyMemento myMemento)
{
this._state1 = myMemento.State1;
this._state2 = myMemento.State2;
this._state3 = myMemento.State3;
}
}
//备忘录类,用于保存当前状态
public class MyMemento
{
private string _state1;
private int _state2;
private decimal _state3;
public MyMemento(string state1,int state2,decimal state3)
{
this._state1 = state1;
this._state2 = state2;
this._state3 = state3;
}
public MyMemento()
{
}
public decimal State3
{
get { return _state3; }
set { _state3 = value; }
}
public int State2
{
get { return _state2; }
set { _state2 = value; }
}
public string State1
{
get { return _state1; }
set { _state1 = value; }
}
}
//将备忘录对象保存到外部类
public class ExternalSave
{
private MyMemento myMemento;
public MyMemento MyMemento
{
get { return myMemento; }
set { myMemento = value; }
}
}
}
客户端代码:
private void btn_Memento_Click(object sender, EventArgs e)
{
MyAim aim = new MyAim();
aim.DisplayState();
ExternalSave external = new ExternalSave();
external.MyMemento = aim.SaveState();
aim.SetState();
aim.DisplayState();
aim.RecoveryState(external.MyMemento);
aim.DisplayState();
}
{
MyAim aim = new MyAim();
aim.DisplayState();
ExternalSave external = new ExternalSave();
external.MyMemento = aim.SaveState();
aim.SetState();
aim.DisplayState();
aim.RecoveryState(external.MyMemento);
aim.DisplayState();
}