-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memento.cpp
76 lines (55 loc) · 1.42 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
/*
* Memento pattern
*
* Intent: Lets you save and restore previous state of object without revealing
* the details of its implementation.
*/
#include <iostream>
#include <memory>
#include <string>
struct State {
int state_number = 0;
std::string state_name = "Unknown";
std::string data = "Undefined";
};
// Mement provides a way to retrieve memento's data.
// However, it doesn't expose the Originator's state.
class Memento {
protected:
virtual void set_state(State& state) = 0;
virtual State get_state() = 0;
};
class Orignator;
class MementoOriginator : public Memento {
public:
MementoOriginator() = default;
explicit MementoOriginator(const State& state) : _state(state) {}
explicit MementoOriginator(State&& state) : _state(std::move(state)) {}
friend class Orignator;
private:
void set_state(State& state) override {
_state = state;
}
State get_state() override {
return _state;
}
private:
State _state;
};
class Originator {
private:
std::unique_ptr<MementoOriginator> _memento;
State _state;
public:
friend class MementoOriginator;
explicit Originator(State& state) : _state(state), _memento() {}
std::unique_ptr<MementoOriginator> get_memento() {
return std::make_unique<MementoOriginator>(_state);
}
void set_memento(MementoOriginator* memento) {
_state = memento.;
}
};
int main() {
return 0;
}