Example #1
0
        static void Main(string[] args)
        {
            Originator o = new Originator();

            o.State = "On";

            Caretaker c = new Caretaker();

            c.Memento = o.CreateMemento();

            o.State = "Off";
            if (!o.State.ToUpper().Equals(c.Memento.State.ToUpper()))
            {
                o.SetMemento(c.Memento);
            }

            Console.WriteLine("Originator's state = " + o.State);

            Console.ReadKey();
        }
        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();
        }
Example #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();
        }
Example #4
0
 //save state of the originator
 public static void SaveState(Originator <T> orig)
 {
     mementoList.Add(orig.CreateMemento());
 }