大话设计模式C++实现-第18章-备忘录模式

一、UML图


关键词:备份内容到Memento,用的时候再从Memento中取出来,Caretaker负责管理这些操作。


二、概念

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


三、说明

角色:

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

(2)Memento(备忘录):负责存储Originator对象的内部状态,并可以防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,他只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问先前状态所需的所有数据。

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

什么时候用?

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

与命令模式的关系?

如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。

四、C++实现

(1)Memento.h

#ifndef MEMENTO_H
#define MEMENTO_H

#include <iostream>
#include <string>

//Memento类,备忘录,此处为角色状态存储箱RoleStateMemento class
class RoleStateMemento
{
private:
	//生命力
	int vit;
	//攻击力
	int atk;
	//防御力
	int def;
public:
	RoleStateMemento(int vit,int atk,int def)
	{
		this->vit=vit;
		this->atk=atk;
		this->def=def;
	}
	int GetVitality()
	{
		return vit;
	}
	void SetVitality(int vit)
	{
		this->vit=vit;
	}
	int GetAttack()
	{
		return atk;
	}
	void SetAttack(int atk)
	{
		this->atk=atk;
	}
	int GetDefense()
	{
		return def;
	}
	void SetDefense(int def)
	{
		this->def=def;
	}
};

//Originator,发起人,此处为游戏角色,GameRole class
class GameRole
{
private:
	//生命力
	int vit;
	//攻击力
	int atk;
	//防御力
	int def;
public:
	int GetVitality()
	{
		return vit;
	}
	void SetVitality(int vit)
	{
		this->vit=vit;
	}
	int GetAttack()
	{
		return atk;
	}
	void SetAttack(int atk)
	{
		this->atk=atk;
	}
	int GetDefense()
	{
		return def;
	}
	void SetDefense(int def)
	{
		this->def=def;
	}

	void GetInitState()
	{
		this->vit=100;
		this->atk=100;
		this->def=100;
	}
	void Fight()
	{
		this->vit=0;
		this->atk=0;
		this->def=0;
	}
	void StateDisplay()
	{
		std::cout<<"当前角色状态:"<<std::endl;
		std::cout<<"体力:"<<this->vit<<std::endl;
		std::cout<<"生命力:"<<this->atk<<std::endl;
		std::cout<<"防御力:"<<this->def<<std::endl<<std::endl;
	}
	//“保存角色状态”方法,将游戏角色的三个状态值通过实例化“角色状态存储箱”返回
	RoleStateMemento* SaveState()
	{
		return new RoleStateMemento(vit,atk,def);
	}
	//“恢复角色状态”方法,可将外部的“角色状态存储箱”中的状态值恢复给游戏角色
	void RocoveryState(RoleStateMemento memento)
	{
		this->vit=memento.GetVitality();
		this->atk=memento.GetAttack();
		this->def=memento.GetDefense();
	}
};

//Caretaker,管理者,此处为游戏角色管理类,RoleStateCaretaker class
class RoleStateCaretaker
{
private:
	RoleStateMemento* memento;
public:
	RoleStateCaretaker()
	{
		memento=NULL;
	}
	~RoleStateCaretaker()
	{
		if(memento!=NULL)
		{
			delete memento;
			memento=NULL;
		}
	}
	RoleStateMemento* GetMemento()
	{
		return memento;
	}
	void SetMemento(RoleStateMemento* memento)
	{
		this->memento=memento;
	}
};

#endif



(2)Client.h

#include "Memento.h"
#include <iostream>
#include <cstdlib>

void main()
{
	//大战Boss前
	GameRole* lixiaoyao=new GameRole();
	lixiaoyao->GetInitState();
	lixiaoyao->StateDisplay();

	//保存进度
	RoleStateCaretaker* stateAdmin=new RoleStateCaretaker();
	stateAdmin->SetMemento(lixiaoyao->SaveState());

	//大战Boss时,损耗严重
	lixiaoyao->Fight();
	lixiaoyao->StateDisplay();

	//恢复之前状态               
	lixiaoyao->RocoveryState(*stateAdmin->GetMemento());
	lixiaoyao->StateDisplay();

	delete lixiaoyao;
	delete stateAdmin;
	system("pause");
}


(3)运行截图



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值