public bool Save(string name, War3 war3) { if (DictionaryWar3.ContainsKey(name)) { return(false); } DictionaryWar3.Add(name, war3); return(true); }
static void Main(string[] args) { Console.WriteLine("欢迎大家来到软谋教育.net高级班公开课之设计模式特训课,这里是Eleven老师为大家带来的备忘录模式"); Manager manager = new Manager(); War3 game = new War3(army: "6只食尸鬼", race: "Undead", hero: "死亡骑士 1级", resource: "1000G 500W");//命名参数 game.Show(); manager.Save("刚开始", new War3(army: "6只食尸鬼", race: "Undead", hero: "死亡骑士 1级", resource: "1000G 500W")); ////存档一下 //MementoData memento = new MementoData(new War3(army: "6只食尸鬼", race: "Undead", hero: "死亡骑士 1级", resource: "1000G 500W"));//重新new一个对象 Console.WriteLine("去小怪点MF"); Thread.Sleep(1000 * 5); Console.WriteLine("*******************************************************"); game.Hero = "死亡骑士 2级"; game.Army = "6只食尸鬼 2只蜘蛛"; game.Resource = "2000G 100W"; game.Show(); manager.Save("去小怪点MF", new War3(army: "6只食尸鬼 2只蜘蛛", race: "Undead", hero: "死亡骑士 2级", resource: "2000G 100W")); Console.WriteLine("大决战之前"); Thread.Sleep(1000 * 5); Console.WriteLine("*******************************************************"); game.Hero = "死亡骑士 5级 巫妖 3级 小强 1级"; game.Army = "3只食尸鬼 8只蜘蛛 2只憎恶 1只雕像"; game.Resource = "200G 2000W"; game.Show(); manager.Save("大决战之前", new War3(army: "3只食尸鬼 8只蜘蛛 2只憎恶 1只雕像", race: "Undead", hero: "死亡骑士 5级 巫妖 3级 小强 1级", resource: "200G 2000W")); Console.WriteLine("决战之后:失败"); Thread.Sleep(1000 * 5); Console.WriteLine("*******************************************************"); game.Hero = "死亡骑士 6级"; game.Army = "3只食尸鬼 2只蜘蛛"; game.Resource = "500G 2000W"; game.Show(); Console.WriteLine("*******************************************************"); Console.WriteLine("恢复存档"); manager.Recover("大决战之前").Show(); manager.Recover("去小怪点MF").Show(); manager.Recover("刚开始").Show(); //War3 gameNew = memento.Recover(); //gameNew.Show(); //111111111111 Console.Read(); }
static void Main(string[] args) { try { Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的备忘录模式"); //int i = 12345; //i += 100;//动作 命令和反命令 //List<War3> war3List = new List<War3>(); War3 war3 = new War3() { Race = "Undead", Hero = "Level 1 DK", Army = "5只食尸鬼", Resource = "200G 200W" }; //war3List.Add(war3); //war3.Save(); war3.Save("Start"); war3.Show(); Console.WriteLine("*****MF*******"); //War3 war31 = new War3() //{ // Race = "Undead", // Hero = "3级DK1级Lich", // Army = "5只蜘蛛2只食尸鬼", // Resourse = "500G 300W" //};//全新的对象来备份,太耗资源 war3.Hero = "3级DK1级Lich"; war3.Army = "5只蜘蛛2只食尸鬼"; war3.Resource = "500G 300W"; //war3.Save(); war3.Save("MF"); //war3List.Add(war3); war3.Show(); Console.WriteLine("*****交战*******"); war3.Hero = "4级DK 3级Lich 1级小强"; war3.Army = "1只憎恶6只蜘蛛1只雕像"; war3.Resource = "1000G 1500W"; war3.Save("交战"); //war3.Save(); Console.WriteLine("*****决战*******"); war3.Hero = "5级DK 4级Lich 3级小强"; war3.Army = "2只憎恶8只蜘蛛2只雕像"; war3.Resource = "1000G 1500W"; war3.Save("决战"); //war3.Save(); Console.WriteLine("*****决战后*******"); war3.Hero = "6级DK 6级Lich 3级小强"; war3.Army = "1只憎恶2只蜘蛛1只冰龙"; war3.Resource = "10G 1200W"; war3.Save("决战后"); //war3.Save(); //war3.Load(); war3.Load("Start"); war3.Show(); war3.Load("MF"); war3.Show(); war3.Load("交战"); war3.Show(); war3.Load("决战"); war3.Show(); war3.Load("决战后"); war3.Show(); //Console.WriteLine("********月光宝盒**********"); //war3 = war3List[0]; //war3.Show(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
public bool Save(string name, War3 war3) { return(Memento.Save(name, war3)); }
public MementoData(War3 war3) { this.WAR3 = war3; }