Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Originator o = new Originator();

            o.State = "On"; // 初始化状态
            o.Show();

            Caretaker c = new Caretaker();

            c.Memento = o.CreateMemento();

            // 修改状态
            o.State = "Off";
            o.Show();

            // 恢复状态
            o.SetMemento(c.Memento);
            o.Show();

            Console.Read();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Customer customer = new Customer {
                FirstName = "John", LastName = "Smith"
            };

            Originator originator = new Originator(customer);
            Caretaker  caretaker  = new Caretaker(originator);

            caretaker.Backup();
            customer.FirstName = "Bob";

            caretaker.Backup();
            customer.FirstName = "Jerzy";

            caretaker.ShowHistory();

            Console.WriteLine(customer);

            caretaker.Undo();
            Console.WriteLine(customer);

            caretaker.Undo();
            Console.WriteLine(customer);

            Console.WriteLine(customer);

            customer.BeginEdit();

            customer.FirstName = "Bob";

            Console.WriteLine(customer);

            customer.CancelEdit();

            Console.WriteLine(customer);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Originator originator = new Originator()
            {
                Status      = "张三",
                OtherStatus = "你好"
            };


            Caretaker caretaker = new Caretaker()
            {
                Memento = originator.CreateMemento()
            };

            // 修改
            originator.Status = "李四";
            Console.WriteLine($"修改后的值:{originator.Status}");
            // 撤销
            originator.RecoveryMemento(caretaker.Memento);
            Console.WriteLine($"赋值错误,修复中...");
            Console.WriteLine($"撤销已完成:{originator.Status}");
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            List <string> equipment = new List <string>
            {
                "plecak",
                "latarka",
                "nóż",
                "sznurek",
                "kompas",
            };
            GameCharacter originator = new GameCharacter("Lara Kravt", 0, 0, 1, equipment);
            Caretaker     caretaker  = new Caretaker(originator);

            caretaker.Backup();
            originator.AddEquipment("buty");
            originator.AddEquipment("kurtka");
            originator.RemoveEquipment("kompas");
            caretaker.Backup();

            originator.AddEquipment("mapa");


            Console.WriteLine();
            caretaker.ShowEquipmentHistory();

            Console.WriteLine();
            caretaker.UndoEquipment();

            Console.WriteLine();
            caretaker.UndoEquipment();


            Console.WriteLine();
            Console.WriteLine("W historii mamy jeszcze następujące wpisy:");
            caretaker.ShowEquipmentHistory();
            Console.Read();
        }
Ejemplo n.º 5
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("输入的索引大于集合长度!");
                }
            }
        }
Ejemplo n.º 6
0
        //public void Save()
        //{
        //    War3Memento memento = new War3Memento(this.Hero, this.Army, this.Resource);
        //    Caretaker.SaveWar3Memento(memento);
        //}

        //public void Load()
        //{
        //    War3Memento memento = Caretaker.GetWar3Memento();

        //    this.Army = memento.Army;
        //    this.Hero = memento.Hero;
        //    this.Resource = memento.Resource;
        //}

        public void Save(string name)
        {
            War3Memento memento = new War3Memento(this.Hero, this.Army, this.Resource);

            Caretaker.SaveWar3Memento(name, memento);
        }
Ejemplo n.º 7
0
        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();
        }
Ejemplo n.º 8
0
 public void Save(string name)
 {
     Caretaker <AreaMemento> .SaveArea(name, new AreaMemento(this.AreaName, this.Lng, this.Lat, this.CreateTime));
 }