コード例 #1
0
        public void Demo8()
        {
            Console.WriteLine("Tworzymy postać gry. Obecny stan: Lara Kravt, położenie: (0, 0, 1)");
            GameCharacter originator = new GameCharacter("Lara Kravt", 0, 0, 1, new List <string>());
            Caretaker     caretaker  = new Caretaker(originator);

            caretaker.Backup();
            originator.Move();

            caretaker.Backup();
            originator.Move();

            caretaker.Backup();
            originator.Move();

            caretaker.Backup();

            Console.WriteLine();
            caretaker.ShowHistory();

            Console.WriteLine();
            caretaker.Undo();

            Console.WriteLine();
            caretaker.Undo();


            Console.WriteLine();
            Console.WriteLine("W historii mamy jeszcze następujące wpisy:");
            caretaker.ShowHistory();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Conceptual example");
            // Client code.
            Originator originator = new Originator("Super-duper-super-puper-super.");
            Caretaker  caretaker  = new Caretaker(originator);

            caretaker.Backup();
            originator.DoSomething();

            caretaker.Backup();
            originator.DoSomething();

            caretaker.Backup();
            originator.DoSomething();

            Console.WriteLine();
            caretaker.ShowHistory();

            Console.WriteLine("\nClient: Now, let's rollback!\n");
            caretaker.Undo();

            Console.WriteLine("\n\nClient: Once more!\n");
            caretaker.Undo();

            Console.WriteLine();

            Console.WriteLine("Practical example");

            var momentos           = new List <MementoPractical>();
            var caretakerPractical = new CaretakerPractical(momentos);

            var originatorPractical = new OriginatorPractical(new MementoPractical("AA"));

            Console.WriteLine($"Originator Current State: %s\n {originatorPractical.GetState()}");
            caretakerPractical.AddMemento(originatorPractical.CreateMemento());

            originatorPractical.SetState(new MementoPractical("BB"));
            Console.WriteLine($"Originator Current State: %s\n{ originatorPractical.GetState()}");
            caretakerPractical.AddMemento(originatorPractical.CreateMemento());

            originatorPractical.SetState(new MementoPractical("CC"));
            Console.WriteLine($"Originator Current State: %s\n{originatorPractical.GetState()}");
            caretakerPractical.AddMemento(originatorPractical.CreateMemento());

            originatorPractical.Restorememento(caretakerPractical.GetMenento(1));
            Console.WriteLine($"Restored to State: %s\n{ originatorPractical.GetState()}");

            originatorPractical.Restorememento(caretakerPractical.GetMenento(0));
            Console.WriteLine($"Restored to State: %s\n{ originatorPractical.GetState()}");
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Customer customer = new Customer {
                FirstName = "John", LastName = "Smith"
            };

            Originator originator = new Originator(customer);
            Caretaker  caretaker  = new Caretaker(originator);

            caretaker.Backup();
            customer.FirstName = "Bob";

            caretaker.Backup();
            customer.FirstName = "Jerzy";

            caretaker.ShowHistory();

            Console.WriteLine(customer);

            caretaker.Undo();
            Console.WriteLine(customer);

            caretaker.Undo();
            Console.WriteLine(customer);

            Console.WriteLine(customer);

            customer.BeginEdit();

            customer.FirstName = "Bob";

            Console.WriteLine(customer);

            customer.CancelEdit();

            Console.WriteLine(customer);
        }
コード例 #4
0
        static void Main(string[] args)
        {
            List <string> equipment = new List <string>
            {
                "plecak",
                "latarka",
                "nóż",
                "sznurek",
                "kompas",
            };
            GameCharacter originator = new GameCharacter("Lara Kravt", 0, 0, 1, equipment);
            Caretaker     caretaker  = new Caretaker(originator);

            caretaker.Backup();
            originator.AddEquipment("buty");
            originator.AddEquipment("kurtka");
            originator.RemoveEquipment("kompas");
            caretaker.Backup();

            originator.AddEquipment("mapa");


            Console.WriteLine();
            caretaker.ShowEquipmentHistory();

            Console.WriteLine();
            caretaker.UndoEquipment();

            Console.WriteLine();
            caretaker.UndoEquipment();


            Console.WriteLine();
            Console.WriteLine("W historii mamy jeszcze następujące wpisy:");
            caretaker.ShowEquipmentHistory();
            Console.Read();
        }