备忘录模式(Memento),在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可以将该对象状态恢复到原先保存的状态。
备忘录模式UML类图:
由上图可知,Originator(发起人)类:负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态,Originator可根据需要决定Memento存储Originator的哪些内部状态 。
Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其它对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到Memento的窄接口,它只能将备忘录传递给其它对象,Originator看到一个宽接口允许它访问返回到先前状态所需的所有数据。
Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。
备忘录模式(Memento)实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Memento
{
/*
* 备忘录Memento类,负责存储Originator对象的内部状态,
* 并可防止Originator以外的其它对象访问备忘录。
*/
class Memento
{
private string state;
public Memento(string state)//构造方法将相关数据导入
{
this.state = state;
}
public string getState()//需要保存的数据属性,可以是多个
{
return this.state;
}
}
/*
* Originator发起人类,负责创建一个备忘录Memento,
* 用以记录当前时刻它的内部状态,并可以使用备忘录恢复内部状态。
*/
class Originator
{
private string state;//需要保存属性可能有多个
public string getState()
{
return this.state;
}
public void setState(string state)
{
this.state = state;
}
public Memento CreateMemento()//创建备忘录,将当前需要保存的信息导入并实例化出一个Memento对象
{
return new Memento(this.getState());
}
public void RecoveryMemento(Memento memento)//恢复备忘录,将Memento导入并将相关数据恢复
{
this.state = memento.getState();
}
public void show()//显示数据
{
Console.WriteLine("state:" + this.state);
}
}
/*
* Caretaker负责保存好备忘录Memento
*/
class Caretaker
{
private Memento memento;
public Memento getMemento()//得到备忘录
{
return this.memento;
}
public void setMemento(Memento memento)//设置备忘录
{
this.memento = memento;
}
}
}
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Memento
{
class Program
{
static void Main(string[] args)
{
Originator o = new Originator();
Console.WriteLine("Originator对象o的初始状态...");
o.setState("on");
o.show();
Caretaker c = new Caretaker();
/*
* 保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节
*/
c.setMemento(o.CreateMemento());
Console.WriteLine("Originator对象o改变了状态属性为“off”...");
o.setState("off");
o.show();
Console.WriteLine("恢复初始状态...");
o.RecoveryMemento(c.getMemento());
o.show();
Console.Read();
}
}
}
备忘录模式总结:
(1)使用备忘录模式,也就是将要保存的细节给封装在了Memento中,哪一天要更改保存的细节也不用影响客户端了。
(2)备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。
(3)如果在某个系统中使用命令模式时,需要实现命令模式的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。
(4)在当角色的状态改变的时候有可能这个状态无效,这时,就可以使用暂时存储起来的备忘录将状态复原。
备忘录模式案例——游戏进度备忘
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MementoExample
{
class Memento
{
}
//游戏角色类
class GameRole
{
//生命力
private int vit;
//攻击力
private int atk;
//防御力
private int def;
public int getVit()
{
return this.vit;
}
public void setVit(int vit)
{
this.vit = vit;
}
public int getAtk()
{
return this.atk;
}
public void setAtk(int atk)
{
this.atk = atk;
}
public int getDef()
{
return this.def;
}
public void setzdef(int def)
{
this.def = def;
}
/*设置初始状态*/
public void InitState()
{
this.vit = 100;
this.atk = 100;
this.def = 100;
}
/*当前状态显示*/
public void ShowState()
{
Console.WriteLine("角色当前状态:");
Console.WriteLine("生命力:{0}",this.vit);
Console.WriteLine("攻击力:{0}",this.atk);
Console.WriteLine("防御力:{0}",this.def);
}
/*战斗*/
public void Fight()
{
this.vit = 0;
this.atk = 0;
this.def = 0;
}
/*保存角色状态*/
public RoleStateMemento SaveRoleState()
{
return new RoleStateMemento(this.vit,this.atk,this.def);
}
/*恢复角色状态*/
public void RecoveryState(RoleStateMemento rsm)
{
this.vit = rsm.getVit();
this.atk = rsm.getAtk();
this.def = rsm.getDef();
}
}
/*角色状态备忘录*/
class RoleStateMemento
{
//生命力
private int vit;
//攻击力
private int atk;
//防御力
private int def;
public RoleStateMemento(int vit,int atk,int def)
{
this.vit = vit;
this.atk = atk;
this.def = def;
}
public int getVit()
{
return this.vit;
}
public void setVit(int vit)
{
this.vit = vit;
}
public int getAtk()
{
return this.atk;
}
public void setAtk(int atk)
{
this.atk = atk;
}
public int getDef()
{
return this.def;
}
public void setzdef(int def)
{
this.def = def;
}
}
/*游戏管理者类*/
class RoleStateCaretaker
{
private RoleStateMemento rsm;
public RoleStateMemento getRsm()
{
return this.rsm;
}
public void setRsm(RoleStateMemento rsm)
{
this.rsm = rsm;
}
}
}
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MementoExample
{
class Program
{
static void Main(string[] args)
{
//大战boss前
Console.WriteLine("大战boss前...");
GameRole gr = new GameRole();
gr.InitState();
gr.ShowState();
//保存进度
RoleStateCaretaker rsc = new RoleStateCaretaker();
rsc.setRsm(gr.SaveRoleState());
//大战boss,损耗严重
Console.WriteLine("大战boss,损耗严重...");
gr.Fight();
gr.ShowState();
//恢复进度
Console.WriteLine("恢复进度...");
gr.RecoveryState(rsc.getRsm());
gr.ShowState();
Console.Read();
}
}
}
运行结果:
分享到:
相关推荐
备忘录模式的示例代码和文档,学习备忘录模式的参考资料。
备忘录模式(Memento) 用意:用来存储另一对象内部状态的快照。
C#面向对象设计模式纵横谈(21):(行为型模式) Memento 备忘录模式
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。 备忘录模式java demo
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到保存的状态。
在备忘录模式里,一个备忘录是一个对象,它存储另一个对象(备忘录的原发器)在某个瞬间的内部状态。备忘的目的就是为了以后在需要的时候,可以将原发器对象的状态恢复(undo/rollback)到备忘录所保存的状态。 备忘...
NULL 博文链接:https://edwin492.iteye.com/blog/1124741
C#面向对象设计模式 (行为型模式) Memento 备忘录模式 视频讲座下载
备忘录模式C++实现,若理解有误,请留言指出。
本例包含备忘录模式的C#实现,及一个应用案例,加深理解与运用。
主要为大家详细介绍了C++设计模式之备忘录模式Memento的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
c++设计模式-行为型模式-备忘录模式;qt工程;c++简单源码;备忘录(Memento)模式的定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先...
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。
二、备忘录模式结构图 三、备忘录模式中主要角色 1、备忘录(Memento)角色: 存储发起人(Originator)对象的内部状态,而发起人根据需要决定备忘录存储发起人的哪些内部状态。 备忘录可以保护其内容不被发起人...
备忘录模式(Memento Pattern):不破坏封装性的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样已经后就可将该对象恢复到原先保存的状态 下面是一个备忘录模式的demo: #!/usr/bin/env python # -*- ...
本文以一个简单实例讲述了C#备忘录模式(Memento Pattern)的实现方法。分享给大家供大家参考。具体实现方法如下: 简单来说,备忘录模式就是支持回退操作。假设让一个Notepad支持回退操作,如何实现呢? 首先需要一个...
这是运用Memento设计模式,设计的一个简单实例, 你可以通过工具栏选择绘制不同图形,使用备份创建备忘录,然后选择恢复进行绘制图形的恢复。 http://blog.csdn.net/ziyuanxiazai123/article/details/12421265