设计模式之---备忘录
public class TestClien
{
public void Client()
{
GameRole L = new GameRole();
L.InitialState();
L.ShowState();
Menmeto m= L.SaveState();
L.Fight();
L.ShowState();
L.RecoveryState(m);
L.ShowState();
}
}
public class GameRole
{
private int vitality;
private int defense;
private int attack;
internal void InitialState()
{
vitality = 100;
defense = 100;
attack = 100;
}
internal void Fight()
{
vitality = 0;
defense = 0;
attack = 0;
}
internal void ShowState()
{
Console.WriteLine(this.vitality + this.attack + this.defense);
}
internal Menmeto SaveState()
{
Menmeto m = new Menmeto();
m.Attack = this.attack;
m.Defense = this.defense;
m.Vitality = this.vitality;
return m;
}
internal void RecoveryState(Menmeto m)
{
this.vitality = m.Vitality;
this.defense = m.Defense;
this.attack = m.Attack;
}
}
public class Menmeto
{
private int vitality;
public int Vitality
{
get { return vitality; }
set { vitality = value; }
}
private int defense;
public int Defense
{
get { return defense; }
set { defense = value; }
}
private int attack;
public int Attack
{
get { return attack; }
set { attack = value; }
}
}
写完备忘录模式的代码,发现把聚合menmeto的manager类完全省略了,这个模式也能实现。
它也能解决维护或者记录历史的问题,使得玩家能够还原到前一状态。