您的位置:首页 > 编程语言 > C语言/C++

使用备忘录模式实现游戏进度备份 C++

2016-03-24 08:11 531 查看
备忘录模式:

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

他将需要保存的数据状态封装在了 一个 专有的类中,当需要更改的时候, 也不会影响到客户端的代码。

他适用于功能复杂, 但需要维护或者记录属性历史的类, 或者需要保存的属性只是众多属性中的一小部分的时候, 可以根据保存的 状态信息还原之前的状态信息。

使用备忘录模式, 将复杂的对象的内部信息对其他的对象屏蔽起来。

会有一定的内存资源的消耗。

UML:



运行效果:



code:

roleState.h

#ifndef _ROLESTATE_H_
#define _ROLESTATE_H_

/************************************************************************/
/* 记录角色的状态信息                                                   */
/************************************************************************/
class CRoleState{
public:
CRoleState(){}
CRoleState(int vit, int atk, int def) : vit(vit), atk(atk), def(def){}

int Vit() const { return vit; }
void Vit(int val) { vit = val; }
int Atk() const { return atk; }
void Atk(int val) { atk = val; }
int Def() const { return def; }
void Def(int val) { def = val; }

private:
int vit;
int atk;
int def;
};

#endif // _ROLESTATE_H_


roleTaker.h

#ifndef _ROLESTATETAKER_H_
#define _ROLESTATETAKER_H_

#include "roleState.h"

/************************************************************************/
/* 状态数据的管理者                                                     */
/************************************************************************/
class CRoleStateTaker{
public:
CRoleState State() const { return state; }
void State(CRoleState val) { state = val; }

private:
CRoleState state;
};

#endif // _ROLESTATETAKER_H_


role.h

#ifndef _ROLE_H_
#define _ROLE_H_

#include <iostream>
#include "roleState.h"

using std::cout;
using std::endl;

/************************************************************************/
/* 游戏角色                                                             */
/************************************************************************/
class CRole{
public:
int Vit() const { return vit; }
void Vit(int val) { vit = val; }
int Atk() const { return atk; }
void Atk(int val) { atk = val; }
int Def() const { return def; }
void Def(int val) { def = val; }

//************************************
// Method:    StateDisplay
// FullName:  CRole::StateDisplay
// Access:    public
// Returns:   void
// Qualifier: 显示角色状态信息
//************************************
void StateDisplay(){
cout << "角色当前状态:"
<< "\n\t体力:\t" << vit
<< "\n\t攻击力:\t" << atk
<< "\n\t防御力:\t" << def << endl;
}

// 设置角色初始状态
void SetInitState(){
Vit(100);
Atk(100);
Def(100);
}

// fight
void fight(){
Vit(0);
Atk(0);
Def(0);
}

// 保存状态信息
CRoleState saveState(){
return CRoleState(Vit(), Atk(), Def());
}

// 恢复状态信息
void RestoreState(const CRoleState & state){
Vit(state.Vit());
Atk(state.Atk());
Def(state.Def());
}

private:
int vit;    // 生命力
int atk;    // 攻击力
int def;    // 防御力
};

#endif // _ROLE_H_


main.cpp

#include <iostream>
#include "RoleStateTaker.h"
#include "Role.h"

int main(){
CRole s;
s.SetInitState();
s.StateDisplay();

CRoleStateTaker taker;
taker.State(s.saveState());

s.fight();
s.StateDisplay();

s.RestoreState(taker.State());
s.StateDisplay();

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: