public static void WriteTokens(TokenMachine tokenMachine, Memento memento)
 {
     Console.WriteLine("Token machine:");
     WriteTokens(tokenMachine);
     Console.WriteLine("Memento:");
     WriteTokens(memento);
 }
        public static void WriteTokens(TokenMachine tokenMachine)
        {
            var tokens = tokenMachine.Tokens;

            for (int i = 0; i < tokens.Count; i++)
            {
                Console.WriteLine($"{i}) {tokens[i]}");
            }
        }
示例#3
0
        public void SingleTokenTest()
        {
            var tm = new TokenMachine();
            var m  = tm.AddToken(123);

            tm.AddToken(456);
            tm.Revert(m);
            Assert.That(tm.Tokens.Count, Is.EqualTo(1));
            Assert.That(tm.Tokens[0].Value, Is.EqualTo(123));
        }
        static void Main(string[] args)
        {
            /*
             * This is a trivial example of implementing memento pattern.
             * In this example our bank account methods return a snapshot of it's balance
             * after the operation so that we can save those tokens and restore our account
             * to any state later.
             */
            var ba = new SimpleBankAccount(100);
            var s1 = ba.Deposit(50);  // 150
            var s2 = ba.Deposit(100); // 250

            WriteLine(ba);

            ba.Restore(s1);
            WriteLine(ba);

            ba.Restore(s2);
            WriteLine(ba);

            /*
             * This example illustrates a more advanced example of memento pattern
             * as a form of undo and redo implementation.
             */
            var b = new BankAccount(100);

            b.Deposit(50);
            var as1 = b.Deposit(25);

            WriteLine(b);

            b.Undo();
            WriteLine($"Undo 1 {b}");
            b.Undo();
            WriteLine($"Undo 2 {b}");
            b.Redo();
            WriteLine($"Redo {b}");
            b.Restore(as1);
            WriteLine($"Restore {b}");

            var tm        = new TokenMachine();
            var testToken = new Token(1);
            var h1        = tm.AddToken(testToken);    // 1
            var h2        = tm.AddToken(new Token(3)); // 1, 3
            var h3        = tm.AddToken(new Token(5)); // 1,3,5

            WriteLine(tm);
            testToken.Value = 10;
            WriteLine(tm); // 10,3,5
            tm.Revert(h2);
            WriteLine(tm); // 1, 3
        }
        static void Main(string[] args)
        {
            var tm = new TokenMachine();
            var t  = new Token(111);

            var m = tm.AddToken(t);

            WriteTokens(tm, m);

            t.Value = 333;

            WriteTokens(tm, m);
        }
示例#6
0
        public void TwoTokenTest()
        {
            var tm = new TokenMachine();

            tm.AddToken(1);
            var m = tm.AddToken(2);

            tm.AddToken(3);
            tm.Revert(m);
            Assert.That(tm.Tokens.Count, Is.EqualTo(2));
            Assert.That(tm.Tokens[0].Value, Is.EqualTo(1),
                        $"First token should have value 1, you got {tm.Tokens[0].Value}");
            Assert.That(tm.Tokens[1].Value, Is.EqualTo(2));
        }
示例#7
0
        public void FiddledTokenTest()
        {
            var tm = new TokenMachine();

            Console.WriteLine("Made a token with value 111 and kept a reference");
            var token = new Token(111);

            Console.WriteLine("Added this token to the list");
            tm.AddToken(token);
            var m = tm.AddToken(222);

            Console.WriteLine("Changed this token's value to 333 :)");
            token.Value = 333;
            tm.Revert(m);

            Assert.That(tm.Tokens.Count, Is.EqualTo(2),
                        $"At this point, token machine should have exactly two tokens, you got {tm.Tokens.Count}");

            Assert.That(tm.Tokens[0].Value, Is.EqualTo(111),
                        $"You got the token value wrong here. Hint: did you init the memento by value?");
        }