一、什么是备忘录模式
就跟这名字一样,可以读取任何时候存储的记录。
二、备忘录模式的作用
就像一个后悔药,在以前的单机游戏中应该很常见,在玩的过程中会有存档,这就相当于是备忘录,保存当时的所有可变状态,方便回滚操作。
三、备忘录模式的使用场景
在有需要回滚操作的时候应该能用上,平时应该用的少,暂时想不出来。。
四、如何实现备忘录模式
我这里就以游戏存档为例,红色警戒存档:
主程序:
namespace Memento
{
class Program
{
static void Main(string[] args)
{
Red red = new Red("中国")
{
Architecture = "基地",
Troops = "4辆犀牛,6个动员兵",
Money = "6000"
};
red.Save("开始");
red.Show();
//Console.WriteLine("*******************存档1*******************");
red.Architecture += ",发电厂,采矿场,兵营,重工";
red.Troops = "6辆犀牛,1辆防空车,6个动员兵";
red.Money = "1000";
red.Save("存档1");
//Console.WriteLine("*******************存档2*******************");
red.Architecture += ",雷达";
red.Troops = "10辆犀牛,2辆防空车,10个动员兵";
red.Money = "1500";
red.Save("存档2");
//Console.WriteLine("*******************存档3*******************");
red.Architecture += ",实验室";
red.Troops = "2辆犀牛,2辆防空车";
red.Money = "500";
red.Save("存档3");
//Console.WriteLine("*******************存档4*******************");
red.Architecture += ",核弹";
red.Troops = "10辆犀牛,2辆防空车,5个动员兵,10条狗";
red.Money = "100";
red.Save("存档4");
Console.WriteLine("");
Console.WriteLine("*******************开始*******************");
red.Load("开始");
red.Show();
Console.WriteLine("");
Console.WriteLine("*******************存档1*******************");
red.Load("存档1");
red.Show();
Console.WriteLine("");
Console.WriteLine("*******************存档2*******************");
red.Load("存档2");
red.Show();
Console.WriteLine("");
Console.WriteLine("*******************存档3*******************");
red.Load("存档3");
red.Show();
Console.WriteLine("");
Console.WriteLine("*******************存档4*******************");
red.Load("存档4");
red.Show();
}
}
}
游戏信息Red.cs:
namespace Memento
{
public class Red
{
public string Country { get; private set; }
public string Architecture { get; set; }
public string Troops { get; set; }
public string Money { get; set; }
public Red(string country)
{
Country = country;
}
public void Show()
{
Console.WriteLine($"国家:{this.Country}");
Console.WriteLine($"建筑:{this.Architecture}");
Console.WriteLine($"部队:{this.Troops}");
Console.WriteLine($"Resource:{this.Money}");
}
public void Save(string name)
{
RedMemento memento = new RedMemento(this.Architecture, this.Troops, this.Money);
KeepFile.Save(name, memento);
}
public void Load(string name)
{
RedMemento memento = KeepFile.Get(name);
this.Architecture = memento.Architecture;
this.Troops = memento.Troops;
this.Money = memento.Money;
}
}
}
游戏可变信息RedMemento.cs:
namespace Memento
{
public class RedMemento
{
public string Architecture { get; set; }
public string Troops { get; set; }
public string Money { get; set; }
public RedMemento(string architecture, string troops, string money)
{
this.Architecture = architecture;
this.Troops = troops;
this.Money = money;
}
}
}
存档KeepFile.cs:
namespace Memento
{
public class KeepFile
{
private static Dictionary<string, RedMemento> _Dictionary = new Dictionary<string, RedMemento>();
public static void Save(string name, RedMemento redMemento)
{
_Dictionary.Add(name, redMemento);
}
public static RedMemento Get(string name)
{
if (_Dictionary.ContainsKey(name))
return _Dictionary[name];
else
throw new Exception("未找到存档");
}
}
}
以上为本章所有内容。
完。