MCPcopy Create free account
hub / github.com/Apress/design-patterns-in-modern-cpp / BankAccountCommand

Class BankAccountCommand

Behavioral/Command/command.cpp:37–74  ·  view source on GitHub ↗

should really be BankAccountCommand

Source from the content-addressed store, hash-verified

35
36// should really be BankAccountCommand
37struct BankAccountCommand : Command
38{
39 BankAccount& account;
40 enum Action { deposit, withdraw } action;
41 int amount;
42
43 BankAccountCommand(BankAccount& account,
44 const Action action, const int amount)
45 : account(account), action(action), amount(amount) {}
46
47 void call() const override
48 {
49 switch (action)
50 {
51 case deposit:
52 account.deposit(amount);
53 break;
54 case withdraw:
55 account.withdraw(amount);
56 break;
57 default: break;
58 }
59 }
60
61 void undo() const override
62 {
63 switch (action)
64 {
65 case withdraw:
66 account.deposit(amount);
67 break;
68 case deposit:
69 account.withdraw(amount);
70 break;
71 default: break;
72 }
73 }
74};
75
76// vector doesn't have virtual dtor, but who cares?
77struct CompositeBankAccountCommand

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected