static void Main(string[] args)
        {
            Console.WriteLine("***Memento Pattern Demo***\n");
            //Originator is initialized with a state
            Originator originatorObject = new Originator();
            Memento    mementoObject;

            originatorObject.State = " Initial state ";
            //Console.WriteLine("Current State : {0} ", originatorObject.State);

            mementoObject = originatorObject.GetTheMemento();
            //Making a new state
            originatorObject.State = " Intermediary state ";

            // Restore to the previous state
            originatorObject.RevertToState(mementoObject);
            //Console.WriteLine("Current State : {0} ", originatorObject.State);

            // Wait for user's input
            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***Memento Pattern QAs***");
            Console.WriteLine("***Demonstration-Caretaker is using multiple restore points * **\n");
            //Originator is initialized with a state
            Originator      originatorObject = new Originator();
            Memento         mementoObject;
            IList <Memento> savedStates = new List <Memento>();

            Console.WriteLine("A new set of verification");
            //State-1
            originatorObject.State = "State-1";
            savedStates.Add(originatorObject.GetTheMemento());
            //State-2
            originatorObject.State = "State-2";
            savedStates.Add(originatorObject.GetTheMemento());
            //State-3
            originatorObject.State = "State-3";
            savedStates.Add(originatorObject.GetTheMemento());
            //State-4 which is not saved
            originatorObject.State = "State-4";
            //Available restore points
            Console.WriteLine("Currently available restore points are :");
            foreach (Memento m in savedStates)
            {
                Console.WriteLine(m.State);
            }
            //Roll back starts...
            Console.WriteLine("Started restoring process...");
            for (int i = savedStates.Count; i > 0; i--)
            {
                mementoObject       = originatorObject.GetTheMemento();
                mementoObject.State = savedStates[i - 1].State;
                originatorObject.RevertToState(mementoObject);
            }
            // Wait for user
            Console.ReadKey();
        }