Example #1
0
 // 将备忘录中的数据备份导入到联系人列表中
 public void RestoreMemento(ContactMemento memento)
 {
     if (memento != null)
     {
         this.ContactPersons = memento.ContactPersonBack;
     }
 }
 public void RestoreMemento(ContactMemento memento)
 {
     // 下面这种方式是错误的,因为这样传递的是引用
     // 则删除一次可以恢复,但恢复之后再删除的话就恢复不了.
     // 所以应该传递contactPersonBack的深拷贝,深拷贝可以使用序列化来完成
     this.ContactPersons = memento.contactPersonBack;
 }
        public void RestoreMemento(ContactMemento memento)
        {
            //this.ContactPersons = memento.contactPersonBack;
            var contactPersons = Serializer.Deserialize <List <ContactPerson> >(Serializer.Serialize(memento.contactPersonBack));

            this.ContactPersons = contactPersons;
        }
Example #4
0
        public void MementoTesting()
        {
            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);
            }
            int index = 0;

            while (index < 2)
            {
                Console.Write("请输入数字,按窗口的关闭键退出:");

                try
                {
                    index++;
                    //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("输入的索引大于集合长度!");
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            ////Struture for memento pattern
            //Originator origiator = new Originator();
            //origiator.State = "ON";

            //CareTaker careTaker = new CareTaker();
            //careTaker.Memento = origiator.CreateMemento();

            //origiator.State = "OFF";
            //origiator.SetMemento(careTaker.Memento);

            List <ContactPerson> contactPersons = new List <ContactPerson>()
            {
                new ContactPerson()
                {
                    Name = "Learning Hard", MobileNum = "123445"
                },
                new ContactPerson()
                {
                    Name = "Tony", MobileNum = "234565"
                },
                new ContactPerson()
                {
                    Name = "Jock", MobileNum = "231455"
                }
            };
            MobileOwner owner = new MobileOwner(contactPersons);

            owner.ShowContacts();

            CareTaker careTaker = new CareTaker();

            careTaker.ContactMementos.Add(DateTime.Now.ToString(), owner.CreateMemento());

            //Remove some contacts;
            owner.ContactPersons.RemoveAt(2);
            owner.ShowContacts();

            Thread.Sleep(1000);
            careTaker.ContactMementos.Add(DateTime.Now.ToString(), owner.CreateMemento());

            Console.WriteLine("Restore Date Index:");
            var keyCollections = careTaker.ContactMementos.Keys;

            foreach (var key in keyCollections)
            {
                Console.WriteLine(key);
            }
            while (true)
            {
                int index = -1;
                try
                {
                    index = Int32.Parse(Console.ReadLine());
                }
                catch
                {
                    Console.WriteLine("The format is wrong");
                    continue;
                }

                ContactMemento contactMentor = null;
                if (index < keyCollections.Count && careTaker.ContactMementos.TryGetValue(keyCollections.ElementAt(index), out contactMentor))
                {
                    owner.RestoreMemento(contactMentor);
                    owner.ShowContacts();
                }
                else
                {
                    Console.WriteLine("");
                }
            }



            Console.Read();
        }
 /// <summary>
 /// 恢复备份
 /// </summary>
 /// <param name="memento"></param>
 public void RestoreMemento(ContactMemento memento)
 {
     this._contactPersons = memento.GetMemento();
 }
        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.ContactPersonDictionary.Add(DateTime.Now.ToString(), mobileOwner.CreateContactMemento());

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

            Thread.Sleep(1000);
            caretaker.ContactPersonDictionary.Add(DateTime.Now.ToString(), mobileOwner.CreateContactMemento());
            int count = caretaker.ContactPersonDictionary.Count;

            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("{0} {1}", i + 1, caretaker.ContactPersonDictionary.Keys.ElementAt(i));
            }
            while (true)
            {
                Console.Write("请输入数字,按窗口的关闭键退出:");

                int index = -1;
                try
                {
                    index = Int32.Parse(Console.ReadLine());
                }
                catch
                {
                    Console.WriteLine("输入的格式错误");
                    continue;
                }

                ContactMemento contactMentor = null;
                if (index < count)
                {
                    contactMentor = caretaker.ContactPersonDictionary.Values.ElementAt(index);
                    mobileOwner.RestoreMemento(contactMentor);
                    mobileOwner.Show();
                }
                else
                {
                    Console.WriteLine("输入的索引大于集合长度!");
                }
            }
            Console.Read();
        }
Example #8
0
 /// <summary>
 /// 从备忘录回复联系人
 /// </summary>
 /// <param name="memento"></param>
 public void RestoreMemento(ContactMemento memento)
 {
     Contacts = memento.ContactListBack;
 }