Exemple #1
0
        static void Main(string[] args)
        {
            CareTaker ct = new CareTaker();

            Persona p = new Persona();

            p.SetNombre("Matias");
            p.SetNombre("Juan");

            ct.AgregarMemento(p.SaveToMemento());

            p.SetNombre("Rodrigo");

            ct.AgregarMemento(p.SaveToMemento());

            p.SetNombre("Fede");

            MementoClass m1 = ct.GetMemento(0);
            MementoClass m2 = ct.GetMemento(1);

            Console.WriteLine("Memento 1: {0}", m1.GetSavedState());
            Console.WriteLine("Memento 2: {0}", m2.GetSavedState());

            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var prospect = new SalesProspect
            {
                CustomerNumber = 1,
                Name           = "Erno Perälä",
                Address        = "<unknown>",
                CustomerType   = "Lead"
            };

            Console.WriteLine("Start");
            Console.WriteLine("-----");
            Console.WriteLine("{0}", prospect.ToString());

            var careTaker = new CareTaker();

            careTaker.StoreState(prospect.SaveMemento());

            // change prospect data
            prospect.Address = "Ernolantie 6";
            careTaker.StoreState(prospect.SaveMemento());

            Console.WriteLine("After first change");
            Console.WriteLine("------------------");
            Console.WriteLine("{0}", prospect.ToString());

            // change prospect data again
            prospect.CustomerType = "Prospect";
            careTaker.StoreState(prospect.SaveMemento());

            Console.WriteLine("After second change");
            Console.WriteLine("------------");
            Console.WriteLine("{0}", prospect.ToString());


            // restore to previous state
            prospect.RestoreMemento(careTaker.GetMemento());

            Console.WriteLine("After first restore");
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}", prospect.ToString());

            // restore to previous state
            prospect.RestoreMemento(careTaker.GetMemento());

            Console.WriteLine("After second restore");
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}", prospect.ToString());

            Console.ReadLine();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var prospect = new SalesProspect
            {
                CustomerNumber = 1,
                Name = "Erno Perälä",
                Address = "<unknown>",
                CustomerType = "Lead"
            };

            Console.WriteLine("Start");
            Console.WriteLine("-----");
            Console.WriteLine("{0}", prospect.ToString());

            var careTaker = new CareTaker();
            careTaker.StoreState(prospect.SaveMemento());

            // change prospect data
            prospect.Address = "Ernolantie 6";
            careTaker.StoreState(prospect.SaveMemento());

            Console.WriteLine("After first change");
            Console.WriteLine("------------------");
            Console.WriteLine("{0}", prospect.ToString());

            // change prospect data again
            prospect.CustomerType = "Prospect";
            careTaker.StoreState(prospect.SaveMemento());

            Console.WriteLine("After second change");
            Console.WriteLine("------------");
            Console.WriteLine("{0}", prospect.ToString());

            // restore to previous state
            prospect.RestoreMemento(careTaker.GetMemento());

            Console.WriteLine("After first restore");
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}", prospect.ToString());

            // restore to previous state
            prospect.RestoreMemento(careTaker.GetMemento());

            Console.WriteLine("After second restore");
            Console.WriteLine("-------------------");
            Console.WriteLine("{0}", prospect.ToString());

            Console.ReadLine();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Originator originator = new Originator("stato 1");
            //CareTaker ct = new CareTaker(originator.SaveMemento());
            CareTaker ct = new CareTaker();

            ct.AddMemento(originator.SaveMemento());
            Console.WriteLine("stato: " + originator.Text);

            originator.Text = "stato modificato";
            Console.WriteLine("stato: " + originator.Text);
            ct.AddMemento(originator.SaveMemento());

            originator.Text = "stato modificato2";
            Console.WriteLine("stato: " + originator.Text);
            ct.AddMemento(originator.SaveMemento());

            originator.Text = "stato modificato3";
            Console.WriteLine("stato: " + originator.Text);

            originator.RestoreMemento(ct.GetMemento());
            Console.WriteLine("stato: " + originator.Text);

            originator.RestoreMemento(ct.GetMemento());
            Console.WriteLine("stato: " + originator.Text);

            //originator.RestoreMemento(ct.Memento);
            //Console.WriteLine("stato: " + originator.Text);

            // The code provided will print ‘Hello World’ to the console.
            // Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
            Console.WriteLine("Hello World!");
            Console.ReadKey();

            // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app!
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (count >= 0)
            {
                Memento.Memento m = caretaker.GetMemento(count);
                count--;

                doc.restoreMemento(m);

                MessageBox.Show(doc.content);
            }
            else
            {
                MessageBox.Show("No hay elementos para recuperar.");
            }
        }
Exemple #6
0
        public static void Main()
        {
            Originator originator = new Originator();
            CareTaker  careTaker  = new CareTaker();

            originator.ChangeState(new State("状态一"));
            originator.ChangeState(new State("状态二"));

            //存储当前状态
            string key = originator.GetState().name;

            careTaker.AddMemento(key, originator.CreateMemento());

            originator.ChangeState(new State("状态三"));

            //恢复到之前备份的某一状态
            originator.RestoreFromMemento(careTaker.GetMemento(key));

            Debug.Log("目前状态:" + originator.GetState().name);
        }