源码可以在这里找到 大话设计模式C++版
//GameRole.h 游戏角色类
#pragma execution_character_set("utf-8")
#include
class GameRole
{
private:
int m_hp; //生命力
int m_atk; //攻击力
int m_def; //防御力
public:
void stateDisplay() {
qDebug() << QString("角色当前状态:");
qDebug() << QString("体力:%1").arg(m_hp);
qDebug() << QString("攻击力:%1").arg(m_atk);
qDebug() << QString("防御力:%1").arg(m_def);
qDebug() << "";
}
void getInitState() {
m_hp = 100;
m_atk = 100;
m_def = 100;
}
void fight() {
m_hp = 0;
m_atk = 0;
m_def = 0;
}
int getHp() const {
return m_hp;
}
void setHp(int hp) {
m_hp = hp;
}
int getAtk() const {
return m_atk;
}
void setAtk(int atk) {
m_atk = atk;
}
int getDef() const {
return m_def;
}
void setDef(int def) {
m_def = def;
}
};
//main.cpp 客户端代码
#include "GameRole.h"
#include
using namespace std;
int main(int argc, char *argv[])
{
//大战Boss前
shared_ptr<GameRole> lixiaoyao(new GameRole());
lixiaoyao->getInitState();
lixiaoyao->stateDisplay();
//保存进度
shared_ptr<GameRole> backup(new GameRole());
backup->setHp(lixiaoyao->getHp());
backup->setAtk(lixiaoyao->getAtk());
backup->setDef(lixiaoyao->getDef());
//大战Boss时,损耗严重
lixiaoyao->fight();
lixiaoyao->stateDisplay();
//恢复之前状态
lixiaoyao->setHp(backup->getHp());
lixiaoyao->setAtk(backup->getAtk());
lixiaoyao->setDef(backup->getDef());
lixiaoyao->stateDisplay();
return 0;
}
运行结果:
"角色当前状态:"
"体力:100"
"攻击力:100"
"防御力:100"
"角色当前状态:"
"体力:0"
"攻击力:0"
"防御力:0"
"角色当前状态:"
"体力:100"
"攻击力:100"
"防御力:100"
//Originator.h 发起人类
#include
#include
#include
#include "Memento.h"
using namespace std;
class Originator
{
private:
QString m_state;
public:
shared_ptr<Memento> CreateMemento() {
return shared_ptr<Memento>(new Memento(m_state));
}
void setMemento(shared_ptr<Memento> memento) {
m_state = memento->getState();
}
void show() {
qDebug() << QString("State = %1").arg(m_state);
}
const QString &getState() const {
return m_state;
}
void setState(const QString &state) {
m_state = state;
}
};
//Memento.h 备忘录类
#include
class Memento
{
private:
QString m_state;
public:
Memento(QString state) : m_state(state) {}
QString getState() {
return m_state;
}
};
//Caretaker.h 管理者类
#include "Memento.h"
#include
using namespace std;
class Caretaker
{
private:
shared_ptr<Memento> m_memento;
public:
const shared_ptr<Memento> &getMemento() const {
return m_memento;
}
void setMemento(const shared_ptr<Memento> &memento) {
m_memento = memento;
}
};
//main.cpp 客户端代码
#include
#include "Originator.h"
#include "Caretaker.h"
using namespace std;
int main(int argc, char *argv[])
{
shared_ptr<Originator> o(new Originator());
o->setState("On");
o->show();
shared_ptr<Caretaker> c(new Caretaker());
c->setMemento(o->CreateMemento());
o->setState("Off");
o->show();
o->setMemento(c->getMemento());
o->show();
return 0;
}
运行结果:
"State = On"
"State = Off"
"State = On"
//GameRole.h 游戏角色类增加的内容
#include "RoleStateMemento.h"
#include
using namespace std;
class GameRole
{
//...
public:
//...
shared_ptr<RoleStateMemento> saveState() {
return shared_ptr<RoleStateMemento>(new RoleStateMemento(m_hp, m_atk, m_def));
}
void recoveryState(shared_ptr<RoleStateMemento> memento) {
m_hp = memento->getHp();
m_atk = memento->getAtk();
m_def = memento->getDef();
}
//...
};
//RoleStateMemento.h 角色状态存储箱类
class RoleStateMemento
{
private:
int m_hp; //生命力
int m_atk; //攻击力
int m_def; //防御力
public:
RoleStateMemento(int hp, int atk, int def) : m_hp(hp), m_atk(atk), m_def(def) {}
int getHp() const {
return m_hp;
}
void setHp(int hp) {
m_hp = hp;
}
int getAtk() const {
return m_atk;
}
void setAtk(int atk) {
m_atk = atk;
}
int getDef() const {
return m_def;
}
void setDef(int def) {
m_def = def;
}
};
//RoleStateCaretaker.h 角色状态管理者
#include
#include "RoleStateMemento.h"
using namespace std;
class RoleStateCaretaker
{
private:
shared_ptr<RoleStateMemento> m_memento;
public:
const shared_ptr<RoleStateMemento> &getMemento() const {
return m_memento;
}
void setMemento(const shared_ptr<RoleStateMemento> &memento) {
m_memento = memento;
}
};
//main.cpp 客户端代码
#include "GameRole.h"
#include
#include "RoleStateCaretaker.h"
using namespace std;
int main(int argc, char *argv[])
{
//大战Boss前
shared_ptr<GameRole> lixiaoyao(new GameRole());
lixiaoyao->getInitState();
lixiaoyao->stateDisplay();
//保存进度
shared_ptr<RoleStateCaretaker> stateAdmin(new RoleStateCaretaker());
stateAdmin->setMemento(lixiaoyao->saveState());
//大战Boss时,损耗严重
lixiaoyao->fight();
lixiaoyao->stateDisplay();
//恢复之前状态
lixiaoyao->recoveryState(stateAdmin->getMemento());
lixiaoyao->stateDisplay();
return 0;
}
运行结果:
"角色当前状态:"
"体力:100"
"攻击力:100"
"防御力:100"
"角色当前状态:"
"体力:0"
"攻击力:0"
"防御力:0"
"角色当前状态:"
"体力:100"
"攻击力:100"
"防御力:100"
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态——《设计模式》GoF
//Memento.cpp
class Memento
{
string state;
public:
Memento(const string& s) : state(s) {}
string getState() const { return state; }
void setState(const string& s) { state = s; }
};
class Originator
{
string state;
//...
public:
Originator() {}
Memento createMomento() {
Memento m(state);
return m;
}
void setMomento(const Memento& m) {
state = m.getState();
}
};
int main()
{
Originator originator;
//捕获对象状态,存储到备忘录
Memento mem = orginator.createMomento();
//... 改变orginator状态
//从备忘录中恢复
orginator.setMomento(mem);
}