《大话设计模式》
备忘录:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态
//memo.h
#ifndef _MEMO_H_
#define _MEMO_H_
#include <iostream>
using namespace std;
class RoleStateMemento
{
public:
RoleStateMemento(int tvit, int tatk, int tdef):vit(tvit), atk(tatk), def(tdef)
{
cout << "In RoleStateMemento's constructor" << endl;
}
RoleStateMemento(const RoleStateMemento &roleState):vit(roleState.vit), atk(roleState.atk), def(roleState.def)
{
cout << "In RoleStateMemento's copy constructor" << endl;
}
~RoleStateMemento()
{
cout << "In RoleStateMemento's deconstructor" << endl;
}
int getVitality();
void setVitality(int vit);
int getAttack();
void setAttack(int atk);
int getDefense();
void setDefense(int def);
private:
int vit;
int atk;
int def;
};
class GameRole
{
public:
int getVitality();
void setVitality(int vit);
int getAttack();
void setAttack(int atk);
int getDefense();
void setDefense(int def);
void StateDisplay();
void GetInitState();
void Fight();
RoleStateMemento *SaveState();
void RecoveryState(RoleStateMemento *memento);
private:
int vit;
int atk;
int def;
};
class RoleStateCaretaker
{
public:
RoleStateCaretaker():memento(NULL){}
RoleStateMemento *getMemento();
void setMemento(RoleStateMemento *mem);
private:
auto_ptr<RoleStateMemento> memento;
};
#endif
//memo.cpp
#include "memo.h"
int GameRole::getVitality()
{
return this->vit;
}
void GameRole::setVitality(int vit)
{
this->vit = vit;
}
int GameRole::getAttack()
{
return this->atk;
}
void GameRole::setAttack(int atk)
{
this->atk = atk;
}
int GameRole::getDefense()
{
return this->def;
}
void GameRole::setDefense(int def)
{
this->def = def;
}
void GameRole::StateDisplay()
{
cout << "角色当前状态:" << endl;
cout << "体力:" << this->vit << endl;
cout << "攻击力:" << this->atk << endl;
cout << "防御力:" << this->def << endl;
}
void GameRole::GetInitState()
{
this->vit = 100;
this->atk = 100;
this->def = 100;
}
void GameRole::Fight()
{
this->vit = 0;
this->atk = 0;
this->def = 0;
}
RoleStateMemento *GameRole::SaveState()
{
return (new RoleStateMemento(vit, atk, def));
}
void GameRole::RecoveryState(RoleStateMemento *memento)
{
this->vit = memento->getVitality();
this->atk = memento->getAttack();
this->def = memento->getDefense();
}
int RoleStateMemento::getVitality()
{
return this->vit;
}
void RoleStateMemento::setVitality(int vit)
{
this->vit = vit;
}
int RoleStateMemento::getAttack()
{
return this->atk;
}
void RoleStateMemento::setAttack(int atk)
{
this->atk = atk;
}
int RoleStateMemento::getDefense()
{
return this->def;
}
void RoleStateMemento::setDefense(int def)
{
this->def = def;
}
RoleStateMemento *RoleStateCaretaker::getMemento()
{
memento.get();
}
void RoleStateCaretaker::setMemento(RoleStateMemento *mem)
{
memento.reset(new RoleStateMemento(*mem));
}
#include "memo.h"
int main()
{
auto_ptr<GameRole> lixiaoyao(new GameRole());
lixiaoyao->GetInitState();
lixiaoyao->StateDisplay();
auto_ptr<RoleStateCaretaker> stateAdmin(new RoleStateCaretaker());
stateAdmin->setMemento(lixiaoyao->SaveState());
lixiaoyao->Fight();
lixiaoyao->StateDisplay();
lixiaoyao->RecoveryState(stateAdmin->getMemento());
lixiaoyao->StateDisplay();
return 0;
}
输出结果:
角色当前状态:
体力:100
攻击力:100
防御力:100
In RoleStateMemento's constructor
In RoleStateMemento's copy constructor
角色当前状态:
体力:0
攻击力:0
防御力:0
角色当前状态:
体力:100
攻击力:100
防御力:100