-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_undo.cpp
More file actions
95 lines (80 loc) · 2.14 KB
/
command_undo.cpp
File metadata and controls
95 lines (80 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
using namespace std;
struct BankAccount {
int balance{0};
int overdraft_limit{-500};
void deposit(int amount) {
balance += amount;
cout << "deposited " << amount << ", balance is now " << balance << "\n";
}
bool withdraw(int amount) {
if (balance - amount >= overdraft_limit) {
balance -= amount;
cout << "withdrew " << amount << ", balance is now " << balance << "\n";
return true; // Required to know if operation succeeded.
}
return false;
}
friend std::ostream &operator<<(std::ostream &os,
const BankAccount &account) {
os << "balance: " << account.balance;
return os;
}
};
struct Command {
bool succeeded{false};
virtual void call() = 0;
virtual void undo() = 0;
};
struct BankAccountCommand : Command {
BankAccount &account;
enum Action { deposit, withdraw } action;
int amount;
BankAccountCommand(BankAccount &account, Action action, int amount)
: account{account}, action{action}, amount{amount} {}
void call() override {
switch (action) {
case deposit:
account.deposit(amount);
succeeded = true;
break;
case withdraw:
succeeded = account.withdraw(amount);
break;
}
}
void undo() override {
if (!succeeded) {
return;
}
switch (action) {
case deposit:
account.withdraw(amount);
break;
case withdraw:
// Dealing with this case forces us to break the open-closed principle,
// as we need information on whether the operation succeeded.
// This can be done by having access to a boolean value on the account.
account.deposit(amount);
break;
}
}
};
int main() {
BankAccount ba;
vector<BankAccountCommand> commands{
BankAccountCommand{ba, BankAccountCommand::deposit, 100},
BankAccountCommand{ba, BankAccountCommand::withdraw, 200},
};
cout << ba << endl;
for (auto &cmd : commands) {
cmd.call();
}
// execute undo operation in reverse order.
for (auto it = commands.rbegin(); it != commands.rend(); ++it) {
it->undo();
}
cout << ba << endl;
return 0;
}