大话设计模式C++ 备忘录模式

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

大话设计模式C++ 备忘录模式

角色:

(1)Originator(发起人):创建盒子,恢复盒子。负责创建一个Memento,用以记录当前时刻它的内部状态,并可以使用备忘录恢复内部状态。Originator可以根据需要决定Memento存储Originator的哪些内部状态。

(2)Memento(备忘录):存储状态的盒子。负责存储Originator对象的内部状态,并可以防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,

他只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问先前状态所需的所有数据。

(3)Caretaker(管理者):将盒子封装。负责保存包备忘录Memento,不能对备忘录的内容进行操作或检查。

什么时候用?

Memento模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到迁移状态。

  1 #include<iostream>
  2 #include<string>
  3 
  4 class RoleStateMemento //状态存储箱
  5 {
  6 private:
  7     int vit;
  8     int atk;
  9     int def;
 10 public:
 11     RoleStateMemento(int v, int a, int d)
 12     {
 13         vit = v; atk = a; def = d;
 14     }
 15     
 16     int getVit(){ return vit; }
 17     void setVit(int v){ vit = v; }
 18 
 19     int getAtk(){ return atk; }
 20     void setAtk(int v){ atk = v; }
 21 
 22     int getDef(){ return def; }
 23     void setDef(int v){ vit = v; }
 24 
 25 
 26 };
 27 
 28 
 29 class GameRole
 30 {
 31 private:
 32     int vit;
 33     int atk;
 34     int def;
 35 
 36 public:
 37     int getVit(){ return vit; }
 38     void setVit(int v){ this->vit = v; }
 39 
 40     int getAtk(){ return atk; }
 41     void setAtk(int v){ this->atk = v; }
 42 
 43     int getDef(){ return def; }
 44     void setDef(int v){ this->vit = v; }
 45 
 46     void GetInitState()
 47     {
 48         this->vit = 100;this->atk = 100;this->def = 100;
 49     }
 50     void Fight()
 51     {
 52         this->vit = 0; this->atk = 0; this->def = 0;
 53     }
 54     void StateDisplay()
 55     {
 56         std::cout << "当前角色状态:" << std::endl;
 57         std::cout << "体力:" << this->vit << std::endl;
 58         std::cout << "生命力:" << this->atk << std::endl;
 59         std::cout << "防御力:" << this->def << std::endl << std::endl;
 60     }
 61     //“保存角色状态”方法,将游戏角色的三个状态值通过实例化“角色状态存储箱”返回
 62     RoleStateMemento* SaveState()
 63     {
 64         return new RoleStateMemento(vit, atk, def);
 65     }
 66     //“恢复角色状态”方法,可将外部的“角色状态存储箱”中的状态值恢复给游戏角色
 67     void RocoveryState(RoleStateMemento memento)
 68     {
 69         this->vit = memento.getVit();
 70         this->atk = memento.getAtk();
 71         this->def = memento.getDef();
 72     }
 73     
 74 };
 75 
 76 
 77 
 78 
 79 //Caretaker,管理者,管理的是存储箱子,封装save state 和 load state
 80 class RoleStateCaretaker
 81 {
 82 private:
 83     RoleStateMemento* memento;
 84 public:
 85     RoleStateCaretaker()
 86     {
 87         memento = NULL;
 88     }
 89     ~RoleStateCaretaker()
 90     {
 91         if (memento != NULL)
 92         {
 93             delete memento;
 94             memento = NULL;
 95         }
 96     }
 97     RoleStateMemento* GetMemento()
 98     {
 99         return memento;
100     }
101     void SetMemento(RoleStateMemento* memento)
102     {
103         this->memento = memento;
104     }
105 
106 
107 };
108 
109 void main()
110 {
111     //大战Boss前
112     GameRole* lixiaoyao = new GameRole();
113     lixiaoyao->GetInitState();
114     lixiaoyao->StateDisplay();
115 
116     //保存进度
117     RoleStateCaretaker* stateAdmin = new RoleStateCaretaker();
118     stateAdmin->SetMemento(lixiaoyao->SaveState());
119 
120     //大战Boss时,损耗严重
121     lixiaoyao->Fight();
122     lixiaoyao->StateDisplay();
123 
124     //恢复之前状态               
125     lixiaoyao->RocoveryState(*stateAdmin->GetMemento());
126     lixiaoyao->StateDisplay();
127 
128     delete lixiaoyao;
129     delete stateAdmin;
130     system("pause");
131 }