forked from micooz/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMementoPattern.cpp
More file actions
79 lines (62 loc) · 1.16 KB
/
Copy pathMementoPattern.cpp
File metadata and controls
79 lines (62 loc) · 1.16 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
#include <iostream>
#include <string>
using namespace std;
class Memento {
private:
friend class Originator;
Memento(const string& st) {
_st = st;
}
void SetState(const string& st) {
_st = st;
}
string GetState() {
return _st;
}
private:
string _st;
};
class Originator {
public:
Originator() {
_mt = nullptr;
}
Originator(const string &st) {
_st = st;
_mt = nullptr;
}
Memento* CreateMemento() {
return new Memento(_st);
}
void SetMemento(Memento* mt) {
_mt = mt;
}
void RestoreToMemento(Memento* mt) {
_st = mt->GetState();
}
string GetState() {
return _st;
}
void SetState(const string& st) {
_st = st;
}
void PrintState() {
cout << _st << "..." << endl;
}
private:
string _st;
Memento *_mt;
};
int main() {
Originator *o = new Originator();
o->SetState("old");
o->PrintState();
Memento *m = o->CreateMemento();
o->SetState("new");
o->PrintState();
o->RestoreToMemento(m);
o->PrintState();
delete o;
delete m;
return 0;
}