014 --- 第18章 备忘录模式

简述:

  备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

  备忘录模式包括:发起人类、备忘录类、管理者类。

    发起人类:负责创建一个备忘录对象,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。

    备忘录类:负责存储发起人对象的内部状态,并可防止发起人类以外的其他对象访问备忘录对象。

    管理者类:负责保存好备忘录对象。

 

应用场景:需要维护或记录历史属性时使用备忘录模式,命令模式的撤销功能可以使用备忘录模式实现

 

注:开发环境调整为VS2017,操作系统win11

备忘录模式:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 
 6 // 备忘录模式
 7 // 备忘录类
 8 class CMemento
 9 {
10 private:
11     string m_szState;
12 
13 public:
14     CMemento(string szState) { m_szState = szState; }
15 
16     string GetState() { return m_szState; }
17 };
18 // 发起人类
19 class COriginator
20 {
21 private:
22     string m_szState;
23     CMemento* m_pMemento;
24 
25 public:
26     ~COriginator()
27     {
28         if (m_pMemento)
29         {
30             delete m_pMemento;
31             m_pMemento = NULL;
32         }
33     }
34 
35     void SetState(string szState) { m_szState = szState; }
36 
37     string GetState() { return m_szState; }
38 
39     CMemento* CreateMemento()
40     {
41         m_pMemento = new CMemento(m_szState);
42         return m_pMemento;
43     }
44 
45     void SetMemento(CMemento* pMemento) { m_szState = pMemento->GetState(); }
46 
47     void Show()
48     {
49         cout << "State = " << m_szState << endl;
50     }
51 };
52 
53 // 管理者类
54 class CCaretaker
55 {
56 private:
57     CMemento* m_pMemento;
58 
59 public:
60     void SetMemento(CMemento* pMemento) { m_pMemento = pMemento; }
61 
62     CMemento* GetMemento() { return m_pMemento; }
63 };
64 
65 int main()
66 {
67     COriginator Originator;
68     Originator.SetState("On");
69     Originator.Show();
70 
71     CCaretaker Caretaker;
72     Caretaker.SetMemento(Originator.CreateMemento());
73 
74     Originator.SetState("Off");
75     Originator.Show();
76 
77     Originator.SetMemento(Caretaker.GetMemento());
78     Originator.Show();
79 
80     system("pause");
81     return 0;
82 }

 

 输出结果:

 

 

例:游戏进度备忘

代码如下:

 

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 
  6 // 角色状态存储类(备忘录类)
  7 class CRoleStateMemento
  8 {
  9 private:
 10     int m_nVit; // 生命力
 11     int m_nAtk; // 攻击力
 12     int m_nDef; // 防御力
 13 
 14 public:
 15     CRoleStateMemento(int nVit, int nAtk, int nDef)
 16     {
 17         m_nVit = nVit;
 18         m_nAtk = nAtk;
 19         m_nDef = nDef;
 20     }
 21     void SetVitality(int nVit) { m_nVit = nVit; }
 22 
 23     int GetVitality() { return m_nVit; }
 24 
 25     void SetAttack(int nAtk) { m_nAtk = nAtk; }
 26 
 27     int GetAttack() { return m_nAtk; }
 28 
 29     void SetDefense(int nDef) { m_nDef = nDef; }
 30 
 31     int GetDefense() { return m_nDef; }
 32 };
 33 
 34 // 游戏角色类(发起人类)
 35 class CGameRole
 36 {
 37 private:
 38     int m_nVit; // 生命力
 39     int m_nAtk; // 攻击力
 40     int m_nDef; // 防御力
 41     CRoleStateMemento* m_pRoleStateMemento;
 42 
 43 public:
 44     ~CGameRole()
 45     {
 46         if (m_pRoleStateMemento)
 47         {
 48             delete m_pRoleStateMemento;
 49             m_pRoleStateMemento = NULL;
 50         }
 51     }
 52     void SetVitality(int nVit) { m_nVit = nVit; }
 53 
 54     int GetVitality() { return m_nVit; }
 55 
 56     void SetAttack(int nAtk) { m_nAtk = nAtk; }
 57 
 58     int GetAttack() { return m_nAtk; }
 59 
 60     void SetDefense(int nDef) { m_nDef = nDef; }
 61 
 62     int GetDefense() { return m_nDef; }
 63 
 64     // 状态显示
 65     void StateDisplay()
 66     {
 67         cout << "角色当前状态:" << endl;
 68         cout << "体力:" << m_nVit << endl;
 69         cout << "攻击力:" << m_nAtk << endl;
 70         cout << "防御力:" << m_nDef << endl << endl;
 71     }
 72 
 73     // 获得初始状态
 74     void GetInitState()
 75     {
 76         m_nVit = 100;
 77         m_nAtk = 100;
 78         m_nDef = 100;
 79     }
 80 
 81     // 战斗
 82     void Fight()
 83     {
 84         m_nVit = 0;
 85         m_nAtk = 0;
 86         m_nDef = 0;
 87     }
 88 
 89     // 保存角色状态
 90     CRoleStateMemento* SaveState()
 91     {
 92         m_pRoleStateMemento = new CRoleStateMemento(m_nVit, m_nAtk, m_nDef);
 93         return m_pRoleStateMemento;
 94     }
 95 
 96     // 恢复角色状态
 97     void RecoveryState(CRoleStateMemento* pRoleStateMemento)
 98     {
 99         m_nVit = pRoleStateMemento->GetVitality();
100         m_nAtk = pRoleStateMemento->GetAttack();
101         m_nDef = pRoleStateMemento->GetDefense();
102     }
103 
104     // ...
105 };
106 
107 // 角色状态管理者类(管理者类)
108 class CRoleStateCaretaker
109 {
110 private:
111     CRoleStateMemento* m_pMemento;
112 
113 public:
114     void SetRoleStateMemento(CRoleStateMemento* pMemento) { m_pMemento = pMemento; }
115 
116     CRoleStateMemento* GetRoleStateMemento() { return m_pMemento; }
117 };
118 
119 int main()
120 {
121     // 大战BOSS前
122     CGameRole GameRole;
123     GameRole.GetInitState();
124     GameRole.StateDisplay();
125 
126     // 保存进度
127     CRoleStateCaretaker RoleStateCaretaker;
128     RoleStateCaretaker.SetRoleStateMemento(GameRole.SaveState());
129 
130     //大战BOSS时,损耗严重
131     GameRole.Fight();
132     GameRole.StateDisplay();
133 
134     // 恢复之前状态
135     GameRole.RecoveryState(RoleStateCaretaker.GetRoleStateMemento());
136     GameRole.StateDisplay();
137 
138     system("pause");
139     return 0;
140 }

 

运行结果:

 

posted @ 2020-09-04 15:05  二是一种不三不亖的范  阅读(105)  评论(0编辑  收藏  举报