private static void Main(string[] args) { //现在有三个人 List <Man> man = new List <Man>() { new Man() { Name = "小A", Age = 18 }, new Man() { Name = "小B", Age = 19 }, new Man() { Name = "小C", Age = 20 }, }; //女神的记录 MobileOwner mobileOwner = new MobileOwner(man); mobileOwner.Show(); //创建备忘录 ContactMemento ConM = mobileOwner.CreateMemento(); //删除女神的记录 mobileOwner.manList.RemoveRange(0, 3); mobileOwner.Show(); //恢复女神的记录 mobileOwner.RestoreMemeoto(ConM); mobileOwner.Show(); Console.ReadLine(); }
static void Main(string[] args) { List<ContactPerson> contactPersons = new List<ContactPerson>() { new ContactPerson(){Name="张三",MobileNumber="123456789"}, new ContactPerson(){Name="李四",MobileNumber="987498416"}, new ContactPerson(){Name="王五",MobileNumber="284589891"} }; MobileOwner owner = new MobileOwner(contactPersons); Administrator admin = new Administrator(); admin.MemetoDictionary.TryAdd(DateTime.Now.ToString(), owner.CreateMemento()); owner.Show(); Console.WriteLine("--------------------------------------"); Console.WriteLine("删掉王五联系人"); Console.WriteLine("--------------------------------------"); owner.ContactPersons.RemoveAt(2); admin.MemetoDictionary.TryAdd(DateTime.Now.ToString(), owner.CreateMemento()); owner.Show(); Console.WriteLine("--------------------------------------"); Console.WriteLine("还原之前备份的联系人列表"); Memento memento = new Memento(null); if (admin.MemetoDictionary.TryGetValue(admin.MemetoDictionary.Keys.First(), out memento)) { owner.RestoreMemeto(memento); Console.WriteLine("--------------------------------------"); owner.Show(); } else Console.WriteLine("获取备份失败"); Console.ReadKey(); }
private static void Main(string[] args) { //现在有三个人 List<Man> man = new List<Man>() { new Man() { Name="小A", Age=18 }, new Man() { Name="小B", Age=19 }, new Man() { Name="小C", Age=20 }, }; //女神的记录 MobileOwner mobileOwner = new MobileOwner(man); mobileOwner.Show(); //创建备忘录 ContactMemento ConM = mobileOwner.CreateMemento(); //删除女神的记录 mobileOwner.manList.RemoveRange(0, 3); mobileOwner.Show(); //恢复女神的记录 mobileOwner.RestoreMemeoto(ConM); mobileOwner.Show(); Console.ReadLine(); }
static void Main(string[] args) { List <ContactPerson> persons = new List <ContactPerson>() { new ContactPerson() { Name = "Learning Hard", MobileNum = "123445" }, new ContactPerson() { Name = "Tony", MobileNum = "234565" }, new ContactPerson() { Name = "Jock", MobileNum = "231455" } }; MobileOwner mobileOwner = new MobileOwner(persons); mobileOwner.Show(); // 创建备忘录并保存备忘录对象 Caretaker caretaker = new Caretaker(); caretaker.ContactMementoDic.Add(DateTime.Now.ToString(), mobileOwner.CreateMemento()); // 更改发起人联系人列表 Console.WriteLine("----移除最后一个联系人--------"); mobileOwner.ContactPersons.RemoveAt(2); mobileOwner.Show(); // 创建第二个备份 Thread.Sleep(1000); caretaker.ContactMementoDic.Add(DateTime.Now.ToString(), mobileOwner.CreateMemento()); // 恢复到原始状态 Console.WriteLine("-------恢复联系人列表,请从以下列表选择恢复的日期------"); var keyCollection = caretaker.ContactMementoDic.Keys; foreach (string k in keyCollection) { Console.WriteLine("Key = {0}", k); } while (true) { Console.Write("请输入数字,按窗口的关闭键退出:"); int index = -1; try { index = Int32.Parse(Console.ReadLine()); } catch { Console.WriteLine("输入的格式错误"); continue; } ContactMemento contactMentor = null; if (index < keyCollection.Count && caretaker.ContactMementoDic.TryGetValue(keyCollection.ElementAt(index), out contactMentor)) { mobileOwner.RestoreMemento(contactMentor); mobileOwner.Show(); } else { Console.WriteLine("输入的索引大于集合长度!"); } } }