static void Main(string[] args)
        {
            var ba         = new BankAccount();
            var cmdDeposit = new BankAccountCommand(ba,
                                                    BankAccountCommand.Action.Deposit, 100);
            var cmdWithdraw = new BankAccountCommand(ba,
                                                     BankAccountCommand.Action.Withdraw, 1000);

            cmdDeposit.Call();
            cmdWithdraw.Call();
            WriteLine(ba);
            cmdWithdraw.Undo();
            cmdDeposit.Undo();
            WriteLine(ba);


            var from = new BankAccount();

            from.Deposit(100);
            var to = new BankAccount();

            var mtc = new MoneyTransferCommand(from, to, 1000);

            mtc.Call();


            // Deposited $100, balance is now 100
            // balance: 100
            // balance: 0

            WriteLine(from);
            WriteLine(to);
        }
Exemplo n.º 2
0
        public override void Call()
        {
            BankAccountCommand last = null;

            foreach (var cmd in this)
            {
                if (last == null || last.Success)
                {
                    cmd.Call();
                    last = cmd;
                }
                else
                {
                    cmd.Undo();
                    break;
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // composite
            var ba         = new BankAccount();
            var cmdDeposit = new BankAccountCommand(ba,
                                                    BankAccountCommand.Action.Deposit, 100);
            var cmdWithdraw = new BankAccountCommand(ba,
                                                     BankAccountCommand.Action.Withdraw, 1000);
            var composite = new CompositeBankAccountCommand(new[] {
                cmdDeposit, cmdWithdraw
            });

            composite.Call();
            Console.WriteLine(ba);

            composite.Undo();
            Console.WriteLine(ba);


            // money transfer
            var from = new BankAccount();

            from.Deposit(100);
            var to = new BankAccount();

            var mtc = new MoneyTransferCommand(from, to, 1000);

            mtc.Call();

            Console.WriteLine(from);
            Console.WriteLine(to);

            mtc.Undo();

            Console.WriteLine(from);
            Console.WriteLine(to);
        }