-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemonto.cpp
More file actions
74 lines (60 loc) · 1.21 KB
/
memonto.cpp
File metadata and controls
74 lines (60 loc) · 1.21 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
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Memento {
private:
// 多状态
int _stat1, _stat2;
public:
Memento() = default;
Memento(int stat1, int stat2) : _stat1(stat1), _stat2(stat2) {}
int stat1() const {
return _stat1;
}
int stat2() const {
return _stat2;
}
};
class Subject {
private:
int _x, _y;
public:
Subject(int x, int y) : _x(x), _y(y) {}
Memento backup() const {
return Memento(_x, _y);
}
void recover(const Memento& mem) {
_x = mem.stat1();
_y = mem.stat2();
}
void output() const {
cout << "x: " << _x << ", y: " << _y << endl;
}
};
class Manager {
private:
// 多备份
map<string, Memento> _memes;
public:
void add(const string& key, const Memento& mem) {
_memes[key] = mem;
}
Memento query(const string& key) {
return _memes[key];
}
};
int main() {
Subject t(10, 20);
Manager manager;
// 创建 01 号备份
manager.add("01", t.backup());
t.output();
// 修改对象
t = Subject(30, 40);
t.output();
// 恢复 01 备份
t.recover(manager.query("01"));
t.output();
return 0;
}