예제 #1
0
        static void Main(string[] args)
        {
            Originator <int> originator = new Originator <int>();

            originator.State = 1;
            CareTaker <int> careTaker = new CareTaker <int>();

            careTaker.Memento = originator.Redo();

            Console.WriteLine(originator.State.ToString());

            originator.State = 2;
            Console.WriteLine(originator.State.ToString());

            originator.Undo(careTaker.Memento);

            Console.WriteLine(originator.State.ToString());
        }
예제 #2
0
        static void Main(string[] args)
        {
            Originator originator = new Originator();
            CareTaker  careTaker  = new CareTaker();

            originator.SetState("State #1");
            originator.SetState("State #2");
            careTaker.Add(originator.SaveStateToMemento());
            originator.SetState("State #3");
            careTaker.Add(originator.SaveStateToMemento());
            originator.SetState("State #4");

            Console.WriteLine($"Current State:{originator.GetState()}");
            originator.GetStateFromMemento(careTaker.Get(0));
            Console.WriteLine($"First saved State:{originator.GetState()}");
            originator.GetStateFromMemento(careTaker.Get(1));
            Console.WriteLine($"Second saved State:{originator.GetState()}");

            Console.ReadKey();
        }
예제 #3
0
        public static void RunTest()
        {
            //Create an originator which creates and gets mementos
            Originator o = new Originator();

            //Set state to On for the originator
            o.state = "On";

            Console.WriteLine("Current state: " + o.state);

            Caretaker c = new Caretaker();

            c.addMemento(o.createMemento());

            o.state = "Off";

            Console.WriteLine("Current state: " + o.state);

            //Stores the snapshot
            o.setMemento(c.getMemento(0));

            Console.WriteLine("Current state: " + o.state);
        }
예제 #4
0
 public Caretaker(Originator originator)
 {
     _originator = originator;
 }