-
Notifications
You must be signed in to change notification settings - Fork 0
/
memento.cpp
81 lines (75 loc) · 2.32 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
/*备忘录模式 有时候类需要保存一些属性,又要保证其封装性
这时候就可以单独分出来一个类来保存这些属性,然后一个管理
这个类的类来对其进行管理 下面的例子就是 一些属性被保存到
memento中,然后一个类来管理memento
可用于维护或者记录属性历史的类,比方说回退功能*/
#include <iostream>
#include <string>
using std::string;
using std::cout;
class roleStateMemento
{
private:
int vit;
int atk;
int def;
public:
roleStateMemento(int v,int a,int d):vit(v),atk(a),def(d){};
int getVit() const {return vit;};
void setVit(int v){vit = v;};
int getAtk()const {return atk;};
void setAtk(int a){atk = a;};
int getDef()const {return def;};
void setDef(int d){def = d;};
};
class gameRole
{
private:
int vit;
int atk;
int def;
public:
gameRole():vit(100),atk(100),def(100){};
gameRole(int v,int a,int d):vit(v),atk(a),def(d){};
void stateShow()const
{
cout<<"生命力"<<vit<<"\n攻击力"<<atk<<"\n防御力"<<def<<"\n";
}
roleStateMemento *saveState()const {return new roleStateMemento(vit,atk,def);};
void recoveryState(const roleStateMemento *memento)
{
vit = memento->getVit();
atk = memento->getAtk();
def = memento->getDef();
}
void fight()
{
vit -= 50;
atk -= 60;
def -= 40;
}
};
class roleSatateCaretaker
{
private:
roleStateMemento *memento;
public:
roleSatateCaretaker():memento(nullptr){};
void setMemento(roleStateMemento *mem){memento = mem;};
roleStateMemento *getMemento(){return memento;};
~roleSatateCaretaker(){delete memento;};
};
int main()
{
gameRole *lixiaoyao = new gameRole();
lixiaoyao->stateShow();
roleSatateCaretaker *stateAdmin = new roleSatateCaretaker();
stateAdmin->setMemento(lixiaoyao->saveState());
lixiaoyao->fight();
lixiaoyao->stateShow();
lixiaoyao->recoveryState(stateAdmin->getMemento());
lixiaoyao->stateShow();
delete lixiaoyao;
delete stateAdmin;
return 0;
}