-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemento.cpp
160 lines (137 loc) · 3.49 KB
/
memento.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <vector>
/***
The Memento interface provides a method to retrieve the memento's state.
***/
class Memento
{
public:
virtual ~Memento() {}
virtual std::string getName() const = 0;
virtual std::string date() const = 0;
virtual std::string state() const = 0;
};
/***
The Concrete Memento contains the infrastructure for storing the Originator's state.
***/
class ConcreteMemento: public Memento
{
private:
std::string _state{};
std::string _date{};
public:
ConcreteMemento(std::string &state)
: _state(state)
{
std::time_t now = std::time(0);
_date = std::ctime(&now);
}
virtual std::string getName() const override
{
return _date + "/ (" + _state.substr(0, 4) + "...)";
}
virtual std::string date() const override { return _date; }
virtual std::string state() const override { return _state; }
};
/***
The Originator holds some important state.
It also defines a method for saving the state inside a Memento,
and a method for retoring the state from it.
***/
class Originator
{
private:
std::string _state{};
std::string generateRandomString(int length = 10)
{
const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
std::string random_string;
for (int i = 0; i < length; i++) {
random_string += alphanum[std::rand() % stringLength];
}
return random_string;
}
public:
Originator(std::string state): _state(state)
{
std::cout << "Originator: My initial state is: " << _state << std::endl;
}
void changeState()
{
_state = generateRandomString();
std::cout << "Originator: Change state to: " << _state << std::endl;
}
void printState() const
{
std::cout << _state << std::endl;
}
Memento *save()
{
return new ConcreteMemento(_state);
}
void restore(Memento* memento)
{
_state = memento->state();
}
};
/***
The Caretaker works with all mementos and the originator via the interface
***/
class Caretaker
{
private:
std::vector<Memento*> _mementos;
Originator* _originator{nullptr};
public:
Caretaker(Originator* originator)
: _originator(originator)
{}
~Caretaker()
{
for(Memento* m: _mementos) delete m;
}
void backup()
{
_mementos.push_back(_originator->save());
}
void undo()
{
if(_mementos.empty()) return;
_originator->restore(_mementos.back());
delete _mementos.back();
_mementos.pop_back();
}
void showHistory() const
{
std::cout << "------Caretaker Hsitory------" << std::endl;
for(Memento* memento: _mementos)
{
std::cout << memento->getName() << std::endl;
}
std::cout << "-----------------------------" << std::endl;
}
};
int main()
{
Originator *originator = new Originator("Super-duper-super-puper-super.");
Caretaker *caretaker = new Caretaker(originator);
caretaker->backup();
originator->changeState();
caretaker->backup();
originator->changeState();
caretaker->backup();
originator->changeState();
caretaker->showHistory();
caretaker->undo();
originator->printState();
caretaker->undo();
originator->printState();
caretaker->undo();
originator->printState();
delete originator;
delete caretaker;
}