static void Main(string[] args) { BankAccount ba = new BankAccount(); var commands = new List <BankAcoountCommand> { new BankAcoountCommand(ba, BankAcoountCommand.Action.Deposit, 100), new BankAcoountCommand(ba, BankAcoountCommand.Action.Withdraw, 1500), }; WriteLine(ba); foreach (var c in commands) { c.Call(); } WriteLine(ba); foreach (var c in Enumerable.Reverse(commands)) { c.Undo(); } WriteLine(ba); var ba2 = new BankAccount2(); var cmdDeposit = new BankAccountCommand(ba2, BankAccountCommand.Action.Deposit, 100); var cmdWithdraw = new BankAccountCommand(ba2, BankAccountCommand.Action.Withdraw, 1000); cmdDeposit.Call(); cmdWithdraw.Call(); WriteLine(ba); cmdWithdraw.Undo(); cmdDeposit.Undo(); WriteLine(ba); var from = new BankAccount2(); from.Deposit(100); var to = new BankAccount2(); var mtc = new MoneyTransferCommand(from, to, 1000); mtc.Call(); // Deposited $100, balance is now 100 // balance: 100 // balance: 0 WriteLine(from); WriteLine(to); }
public MoneyTransferCommand(BankAccount2 from, BankAccount2 to, int amount) { AddRange(new[] { new BankAccountCommand(from, BankAccountCommand.Action.Withdraw, amount), new BankAccountCommand(to, BankAccountCommand.Action.Deposit, amount) }); }
public BankAccountCommand(BankAccount2 account, Action action, int amount) { this.account = account; this.action = action; this.amount = amount; }