备忘录模式
来源:互联网 发布:淘宝图片怎么拍摄 编辑:程序博客网 时间:2024/05/16 15:03
《大话设计模式》
备忘录:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态
//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
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- 备忘录模式
- cocos2d-x学习笔记(3)--重要概念
- linux下i386,i486,i586,i686的区别
- 动画库 CPAnimationSequence
- ATI自动化测试电子杂志2012年7月版下载
- 最大值最小化问题
- 备忘录模式
- 表白失败,还是好好写程序吧
- PHP特级视频教程 笔记心得 第十四集 MySQL分区实验 MySQL分区实战
- 指针之对未声明的地址进行访问
- c#解析json文件的方法
- PHP特级视频教程 笔记心得 第十五集 mysql调优,mysql优化 MySQL基础操作(一)
- iOS App的图标,启动画面及其他
- android模拟器安装软件时报错:device not found
- ASP.Net 实战项目 之 《图书商城》 第一天