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

            originator.SetState("state1");
            originator.CreateMemento();
            Console.WriteLine("CurrentState: " + originator.GetState());
            originator.SetState("state2");
            originator.CreateMemento();
            Console.WriteLine("CurrentState: " + originator.GetState());

            originator.SetMemento(caretaker.GetPreviousMemento());
            Console.WriteLine("RolledBackState: " + originator.GetState());
        }
Example #2
0
        static void Main(string[] args)
        {
            Originator <StateObject> current = new Originator <StateObject>();

            current.SetState(new StateObject {
                Id = 0, Name = "Object 0"
            });
            CareTaker <StateObject> .SaveState(current);

            current.ShowState();

            current.SetState(new StateObject {
                Id = 1, Name = "Object 1"
            });
            CareTaker <StateObject> .SaveState(current);

            current.ShowState();

            current.SetState(new StateObject {
                Id = 2, Name = "Object 2"
            });
            CareTaker <StateObject> .SaveState(current);

            current.ShowState();

            current.SetState(new StateObject {
                Id = 3, Name = "Object 3"
            });
            CareTaker <StateObject> .SaveState(current);

            current.ShowState();

            CareTaker <StateObject> .RestoreState(current, 1);

            current.ShowState();

            Console.ReadKey();
        }