Beispiel #1
0
        public void MementoTest()
        {
            const string originalState = "Super-duper-super-puper-super.";
            // Client code.
            var originator = new Originator(originalState);
            var caretaker  = new Caretaker(originator);

            caretaker.Backup();
            var a = originator.DoSomething();

            caretaker.Backup();
            var b = originator.DoSomething();

            caretaker.Backup();

            var history = caretaker.ShowHistory();

            history.Count.Should().Be(3);
            history[0].Should().Contain(originalState);
            history[1].Should().Contain(a);
            history[2].Should().Contain(b);

            caretaker.Undo();

            history = caretaker.ShowHistory();
            history.Count.Should().Be(2);
            history[0].Should().Contain(originalState);
            history[1].Should().Contain(a);

            caretaker.Undo();

            history = caretaker.ShowHistory();
            history.Count.Should().Be(1);
            history[0].Should().Contain(originalState);
        }
        public static void TestMemento()
        {
            Originator originator = new Originator("Init state");
            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("\n\nClient: Once more!\n");
            caretaker.Undo();

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

            Console.WriteLine();
        }
Beispiel #3
0
        public void Get()
        {
            // 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();
        }
Beispiel #4
0
    static void Main(string[] args)
    {
        Originator originator = new Originator("NusmirSmajovic");
        Caretaker  caretaker  = new Caretaker(originator);

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

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

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

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

        Console.WriteLine("\nClient: gå tillbaka!\n");
        caretaker.Undo();

        Console.WriteLine("\n\nClient: En gång till!\n");
        caretaker.Undo();


        Console.WriteLine("\nClient: gå tillbaka!\n");
        caretaker.Redo();

        Console.WriteLine("\n\nClient: En gång till!\n");
        caretaker.Redo();

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


        Console.WriteLine();
    }
        public void Memento_Pattern_Test()
        {
            var originator = new Originator("super-super-super-super");
            var caretaker  = new Caretaker(originator);

            var obj1 = caretaker.Backup();

            Assert.AreEqual("super-super-super-super", obj1.GetState());

            originator.DoSomething();
            var obj2 = caretaker.Backup();

            Assert.AreNotEqual("super-super-super-super", obj2.GetState());

            var obj3 = caretaker.Undo();

            Assert.AreEqual("super-super-super-super", obj3.GetState());
        }