Exemplo n.º 1
0
 public void RestoreMemento(ContactMemento memento)
 {
     if (memento != null)
     {
         ContactPeople = memento.ContactPeopleBack;
     }
 }
Exemplo n.º 2
0
        public void Calc()
        {
            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.dic.Add(DateTime.Now.ToString(), mobileOwner.CreateMemento());

            // 更改发起人联系人列表
            Console.WriteLine("----移除最后一个联系人--------");
            mobileOwner.ContactPeople.RemoveAt(2);
            mobileOwner.Show();

            caretaker.dic.Add(DateTime.Now.ToString(), mobileOwner.CreateMemento());// 恢复到原始状态
            Console.WriteLine("-------恢复联系人列表,请从以下列表选择恢复的日期------");
            var keyCollection = caretaker.dic.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)
                {
                    mobileOwner.RestoreMemento(contactMentor);
                    mobileOwner.Show();
                }
                else
                {
                    Console.WriteLine("输入的索引大于集合长度!");
                }
            }
        }