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
| int main(const int argc, const char* argv[])
{
// CareTaker
std::shared_ptr<Originator> originator = std::make_shared<Originator>();
std::shared_ptr<CareTaker> careTaker = std::make_shared<CareTaker>();
// first state
std::shared_ptr<Hello> hello = std::make_shared<Hello>("State1: Hello");
std::cout << hello->getMessage() << std::endl;
// save
originator->setState(hello);
careTaker->push(originator);
// second state
hello->setMessage("State2: World!");
std::cout << hello->getMessage() << std::endl;
originator->setState(hello);
careTaker->push(originator);
// third state
hello->setMessage("State3: ABCDEFG");
std::cout << hello->getMessage() << std::endl;
originator->setState(hello);
careTaker->push(originator);
// undo
hello = careTaker->undo(originator); // state 3
std::cout << hello->getMessage() << std::endl;
hello = careTaker->undo(originator); // state 2
std::cout << hello->getMessage() << std::endl;
hello = careTaker->undo(originator); // state 1
std::cout << hello->getMessage() << std::endl;
return 0;
}
|