-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
110 lines (91 loc) · 2.52 KB
/
main.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* @FileName : memento/main.cpp
* @CreateAt : 2022/5/16
* @Author : Inno Fang
* @Email : innofang@yeah.net
* @Description: Simple implementation of memento pattern
*/
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
class Memento {
public:
Memento(std::string &info, std::string &state) : _info(info), _state(state) {
std::time_t now = std::time(0);
_date = std::ctime(&now);
}
std::string info() const {
return _info;
}
std::string state() const {
return _state;
}
std::string date() const {
return _date;
}
friend std::ostream &operator<<(std::ostream &out, const Memento &memento) {
return out << "[ info:" + memento._info << ", state:" + memento._state + ", date:" + memento._date + " ]";
}
private:
std::string _info;
std::string _state;
std::string _date;
};
class Originator {
public:
Originator() = default;
void recordInfoDetail(const std::string &info, const std::string &state) {
_info = info;
_state = state;
}
Memento *save() {
return new Memento(_info, _state);
}
void restore(Memento *memento) {
_state = memento->state();
_info = memento->info();
_date = memento->date();
}
private:
std::string _state;
std::string _info;
std::string _date;
};
class CareTaker {
public:
explicit CareTaker(Originator *originator) : _originator(originator) {}
void backUp() {
_mementos.emplace_back(_originator->save());
}
void undo() {
if (_mementos.empty()) return;
Memento *memento = _mementos.back();
_mementos.pop_back();
_originator->restore(memento);
}
void showHistory() const {
for (auto memento: _mementos) {
std::cout << (*memento) << std::endl;
}
}
private:
Originator *_originator;
std::vector<Memento *> _mementos;
};
int main() {
Originator *originator = new Originator;
CareTaker *careTaker = new CareTaker(originator);
originator->recordInfoDetail("Study at 2:00pm", "Done!");
careTaker->backUp();
originator->recordInfoDetail("Do some sports at 4:00pm", "Have fun!");
careTaker->undo();
originator->recordInfoDetail("Read some books at 4:00pm", "Have to study hard!");
careTaker->backUp();
originator->recordInfoDetail("Eat dinner at 6:00pm", "Delicious");
careTaker->backUp();
careTaker->showHistory();
delete careTaker;
delete originator;
return 0;
}