简述:
备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
备忘录模式包括:发起人类、备忘录类、管理者类。
发起人类:负责创建一个备忘录对象,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。
备忘录类:负责存储发起人对象的内部状态,并可防止发起人类以外的其他对象访问备忘录对象。
管理者类:负责保存好备忘录对象。
备忘录模式:
1 #include <iostream> 2 using namespace std; 3 4 // 备忘录模式 5 // 备忘录类 6 class CMemento 7 { 8 private: 9 string m_szState; 10 11 public: 12 CMemento(string szState) { m_szState = szState; } 13 14 string GetState() { return m_szState; } 15 }; 16 // 发起人类 17 class COriginator 18 { 19 private: 20 string m_szState; 21 CMemento* m_pMemento; 22 23 public: 24 ~COriginator() 25 { 26 if (m_pMemento) 27 { 28 delete m_pMemento; 29 m_pMemento = NULL; 30 } 31 } 32 33 void SetState(string szState) { m_szState = szState; } 34 35 string GetState() { return m_szState; } 36 37 CMemento* CreateMemento() 38 { 39 m_pMemento = new CMemento(m_szState); 40 return m_pMemento; 41 } 42 43 void SetMemento(CMemento* pMemento) { m_szState = pMemento->GetState(); } 44 45 void Show() 46 { 47 cout << "State = " << m_szState << endl; 48 } 49 }; 50 51 // 管理者类 52 class CCaretaker 53 { 54 private: 55 CMemento* m_pMemento; 56 57 public: 58 void SetMemento(CMemento* pMemento) { m_pMemento = pMemento; } 59 60 CMemento* GetMemento() { return m_pMemento; } 61 }; 62 63 int main() 64 { 65 COriginator Originator; 66 Originator.SetState("On"); 67 Originator.Show(); 68 69 CCaretaker Caretaker; 70 Caretaker.SetMemento(Originator.CreateMemento()); 71 72 Originator.SetState("Off"); 73 Originator.Show(); 74 75 Originator.SetMemento(Caretaker.GetMemento()); 76 Originator.Show(); 77 78 system("pause"); 79 return 0; 80 }
输出结果:
例:游戏进度备忘
代码如下:
1 #include <iostream> 2 using namespace std; 3 4 // 角色状态存储类(备忘录类) 5 class CRoleStateMemento 6 { 7 private: 8 int m_nVit; // 生命力 9 int m_nAtk; // 攻击力 10 int m_nDef; // 防御力 11 12 public: 13 CRoleStateMemento(int nVit, int nAtk, int nDef) 14 { 15 m_nVit = nVit; 16 m_nAtk = nAtk; 17 m_nDef = nDef; 18 } 19 void SetVitality(int nVit) { m_nVit = nVit; } 20 21 int GetVitality() { return m_nVit; } 22 23 void SetAttack(int nAtk) { m_nAtk = nAtk; } 24 25 int GetAttack() { return m_nAtk; } 26 27 void SetDefense(int nDef) { m_nDef = nDef; } 28 29 int GetDefense() { return m_nDef; } 30 }; 31 32 // 游戏角色类(发起人类) 33 class CGameRole 34 { 35 private: 36 int m_nVit; // 生命力 37 int m_nAtk; // 攻击力 38 int m_nDef; // 防御力 39 CRoleStateMemento* m_pRoleStateMemento; 40 41 public: 42 ~CGameRole() 43 { 44 if (m_pRoleStateMemento) 45 { 46 delete m_pRoleStateMemento; 47 m_pRoleStateMemento = NULL; 48 } 49 } 50 void SetVitality(int nVit) { m_nVit = nVit; } 51 52 int GetVitality() { return m_nVit; } 53 54 void SetAttack(int nAtk) { m_nAtk = nAtk; } 55 56 int GetAttack() { return m_nAtk; } 57 58 void SetDefense(int nDef) { m_nDef = nDef; } 59 60 int GetDefense() { return m_nDef; } 61 62 // 状态显示 63 void StateDisplay() 64 { 65 cout << "角色当前状态:" << endl; 66 cout << "体力:" << m_nVit << endl; 67 cout << "攻击力:" << m_nAtk << endl; 68 cout << "防御力:" << m_nDef << endl << endl; 69 } 70 71 // 获得初始状态 72 void GetInitState() 73 { 74 m_nVit = 100; 75 m_nAtk = 100; 76 m_nDef = 100; 77 } 78 79 // 战斗 80 void Fight() 81 { 82 m_nVit = 0; 83 m_nAtk = 0; 84 m_nDef = 0; 85 } 86 87 // 保存角色状态 88 CRoleStateMemento* SaveState() 89 { 90 m_pRoleStateMemento = new CRoleStateMemento(m_nVit, m_nAtk, m_nDef); 91 return m_pRoleStateMemento; 92 } 93 94 // 恢复角色状态 95 void RecoveryState(CRoleStateMemento* pRoleStateMemento) 96 { 97 m_nVit = pRoleStateMemento->GetVitality(); 98 m_nAtk = pRoleStateMemento->GetAttack(); 99 m_nDef = pRoleStateMemento->GetDefense(); 100 } 101 102 // ... 103 }; 104 105 // 角色状态管理者类(管理者类) 106 class CRoleStateCaretaker 107 { 108 private: 109 CRoleStateMemento* m_pMemento; 110 111 public: 112 void SetRoleStateMemento(CRoleStateMemento* pMemento) { m_pMemento = pMemento; } 113 114 CRoleStateMemento* GetRoleStateMemento() { return m_pMemento; } 115 }; 116 117 int main() 118 { 119 // 大战BOSS前 120 CGameRole GameRole; 121 GameRole.GetInitState(); 122 GameRole.StateDisplay(); 123 124 // 保存进度 125 CRoleStateCaretaker RoleStateCaretaker; 126 RoleStateCaretaker.SetRoleStateMemento(GameRole.SaveState()); 127 128 //大战BOSS时,损耗严重 129 GameRole.Fight(); 130 GameRole.StateDisplay(); 131 132 // 恢复之前状态 133 GameRole.RecoveryState(RoleStateCaretaker.GetRoleStateMemento()); 134 GameRole.StateDisplay(); 135 136 system("pause"); 137 return 0; 138 }
运行结果: