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

  • 备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。
  • 如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。
  • 使用备忘录可以把复杂的对象内部信息对其他的对象屏蔽起来,当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。

介绍完备忘录模式的定义之后,下面具体看看备忘录模式的结构图:

备忘录模式中主要有三类角色:

  • 发起人角色:记录当前时刻的内部状态,负责创建和恢复备忘录数据。
  • 备忘录角色:负责存储发起人对象的内部状态,在进行恢复时提供给发起人需要的状态。
  • 管理者角色:负责保存备忘录对象。

C#备忘录模式-单个存储点:

namespace 备忘录模式_单个存储点
{
class Program
{
static void Main(string[] args)
{ //大战Boss前
GameRole lixiaoyao = new GameRole();
lixiaoyao.GetInitState();
lixiaoyao.StateDisplay(); //保存进度
RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
stateAdmin.Memento = lixiaoyao.SaveState(); //大战Boss时,损耗严重
lixiaoyao.Fight();
lixiaoyao.StateDisplay(); //恢复之前状态
lixiaoyao.RecoveryState(stateAdmin.Memento); lixiaoyao.StateDisplay(); Console.Read(); }
} class GameRole
{
//生命力
private int vit;
public int Vitality
{
get { return vit; }
set { vit = value; }
} //攻击力
private int atk;
public int Attack
{
get { return atk; }
set { atk = value; }
} //防御力
private int def;
public int Defense
{
get { return def; }
set { def = value; }
} //状态显示
public void StateDisplay()
{
Console.WriteLine("角色当前状态:");
Console.WriteLine("体力:{0}", this.vit);
Console.WriteLine("攻击力:{0}", this.atk);
Console.WriteLine("防御力:{0}", this.def);
Console.WriteLine("");
} //保存角色状态
public RoleStateMemento SaveState()
{
return (new RoleStateMemento(vit, atk, def));
} //恢复角色状态
public void RecoveryState(RoleStateMemento memento)
{
this.vit = memento.Vitality;
this.atk = memento.Attack;
this.def = memento.Defense;
} //获得初始状态
public void GetInitState()
{
this.vit = ;
this.atk = ;
this.def = ;
} //战斗
public void Fight()
{
this.vit = ;
this.atk = ;
this.def = ;
}
} //角色状态存储箱
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 Vitality
{
get { return vit; }
set { vit = value; }
} //攻击力
public int Attack
{
get { return atk; }
set { atk = value; }
} //防御力
public int Defense
{
get { return def; }
set { def = value; }
}
} //角色状态管理者
class RoleStateCaretaker
{
private RoleStateMemento memento; public RoleStateMemento Memento
{
get { return memento; }
set { memento = value; }
}
}
}

C#备忘录模式-多个存储点:

namespace 备忘录模式_多个存储点
{
class Program
{
static void Main(string[] args)
{ //大战Boss前
GameRole lixiaoyao = new GameRole();
lixiaoyao.GetInitState();
lixiaoyao.StateDisplay(); //保存进度
RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
stateAdmin.MementoDic.Add(DateTime.Now.ToString(), lixiaoyao.SaveState()); //大战Boss时,损耗严重
lixiaoyao.Fight1();
lixiaoyao.StateDisplay(); //保存进度
Thread.Sleep();
stateAdmin.MementoDic.Add(DateTime.Now.ToString(), lixiaoyao.SaveState()); //大战Boss时,损耗严重
lixiaoyao.Fight2();
lixiaoyao.StateDisplay(); //恢复之前状态
Console.WriteLine("-------恢复联系人列表,请从以下列表选择恢复的日期------");
var keyCollection = stateAdmin.MementoDic.Keys;
foreach (string k in keyCollection)
{
Console.WriteLine("Key = {0}", k);
}
while (true)
{
Console.Write("请输入数字,按窗口的关闭键退出:"); int index = -;
try
{
index = Int32.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的格式错误");
continue;
} RoleStateMemento memento = null;
if (index < keyCollection.Count && stateAdmin.MementoDic.TryGetValue(keyCollection.ElementAt(index), out memento))
{
lixiaoyao.RecoveryState(memento);
lixiaoyao.StateDisplay();
}
else
{
Console.WriteLine("输入的索引大于集合长度!");
}
}
}
} class GameRole
{
//生命力
private int vit;
public int Vitality
{
get { return vit; }
set { vit = value; }
} //攻击力
private int atk;
public int Attack
{
get { return atk; }
set { atk = value; }
} //防御力
private int def;
public int Defense
{
get { return def; }
set { def = value; }
} //状态显示
public void StateDisplay()
{
Console.WriteLine("角色当前状态:");
Console.WriteLine("体力:{0}", this.vit);
Console.WriteLine("攻击力:{0}", this.atk);
Console.WriteLine("防御力:{0}", this.def);
Console.WriteLine("");
} //保存角色状态
public RoleStateMemento SaveState()
{
return (new RoleStateMemento(vit, atk, def));
} //恢复角色状态
public void RecoveryState(RoleStateMemento memento)
{
this.vit = memento.Vitality;
this.atk = memento.Attack;
this.def = memento.Defense;
} //获得初始状态
public void GetInitState()
{
this.vit = ;
this.atk = ;
this.def = ;
} //战斗
public void Fight1()
{
this.vit = ;
this.atk = ;
this.def = ;
}
public void Fight2()
{
this.vit = ;
this.atk = ;
this.def = ;
}
} //角色状态存储箱
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 Vitality
{
get { return vit; }
set { vit = value; }
} //攻击力
public int Attack
{
get { return atk; }
set { atk = value; }
} //防御力
public int Defense
{
get { return def; }
set { def = value; }
}
} //角色状态管理者
class RoleStateCaretaker
{
// 使用多个备忘录来存储多个备份点
private Dictionary<string, RoleStateMemento> mementoDic;
public Dictionary<string, RoleStateMemento> MementoDic
{
get { return mementoDic; }
set { mementoDic = value; }
}
public RoleStateCaretaker()
{
MementoDic = new Dictionary<string, RoleStateMemento>();
}
}
}

js的备忘录模式-单个存储点:

var GameRole = function(){

        //角色状态存储箱
var roleStateMemento = {}; this.getMemento = function(){
return roleStateMemento;
}; this.setMemento = function(memento){
roleStateMemento = memento;
}; this.getInitState();
};
//状态显示
GameRole.prototype.stateDisplay = function(){
console.log('角色当前状态:');
console.log('体力:' + this.vitality);
console.log('攻击力:' + this.attack);
console.log('防御力:' + this.defense);
};
//保存角色状态
GameRole.prototype.saveState = function(){
this.setMemento({
vitality:this.vitality,
attack:this.attack,
defense:this.defense
});
};
//恢复角色状态
GameRole.prototype.recoveryState = function(){
var memento = this.getMemento();
this.vitality = memento.vitality;
this.attack = memento.attack;
this.defense = memento.defense;
};
//获得初始状态
GameRole.prototype.getInitState = function(){
this.vitality = 100;
this.attack = 100;
this.defense = 100;
};
//战斗
GameRole.prototype.fight = function(){
this.vitality = 0;
this.attack = 0;
this.defense = 0;
}; //调用:
//
//大战Boss前
var gr = new GameRole();
gr.stateDisplay(); //保存进度
gr.saveState(); //大战Boss时,损耗严重
gr.fight();
gr.stateDisplay(); //恢复之前状态
gr.recoveryState();
gr.stateDisplay();

js的备忘录模式-多个存储点:

var GameRole = function(){

        //角色状态存储箱
var roleStateMemento = {}; this.getMemento = function(key){
if(key)
return roleStateMemento[key];
else
return roleStateMemento;
}; this.setMemento = function(key,memento){
roleStateMemento[key] = memento;
}; this.getInitState();
};
//状态显示
GameRole.prototype.stateDisplay = function(){
console.log('角色当前状态:');
console.log('体力:' + this.vitality);
console.log('攻击力:' + this.attack);
console.log('防御力:' + this.defense);
};
//保存角色状态
GameRole.prototype.saveState = function(key){
this.setMemento(key,{
vitality:this.vitality,
attack:this.attack,
defense:this.defense
});
};
//恢复角色状态
GameRole.prototype.recoveryState = function(key){
var memento = this.getMemento(key);
this.vitality = memento.vitality;
this.attack = memento.attack;
this.defense = memento.defense;
};
//获得初始状态
GameRole.prototype.getInitState = function(){
this.vitality = 100;
this.attack = 100;
this.defense = 100;
};
//战斗
GameRole.prototype.fight1 = function(){
this.vitality = 50;
this.attack = 50;
this.defense = 50;
};
GameRole.prototype.fight2 = function(){
this.vitality = 0;
this.attack = 0;
this.defense = 0;
}; //调用:
//
//大战Boss前
var gr = new GameRole();
gr.stateDisplay(); //保存进度
gr.saveState(new Date().getTime()); //大战Boss时,损耗严重
gr.fight1();
gr.stateDisplay(); setTimeout(function(){ //保存进度
gr.saveState(new Date().getTime()); //大战Boss时,损耗严重
gr.fight2();
gr.stateDisplay(); var mementos = gr.getMemento();
for(var key in mementos){
console.log('key = ' + key); //恢复之前状态
gr.recoveryState(key);
gr.stateDisplay();
} },1000);

总结

备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原来状态时,再从备忘录对象中进行获取,如果系统需要提供回滚操作时,使用备忘录模式非常合适。例如文本编辑器的Ctrl+Z撤销操作的实现,数据库中事务操作。

js备忘录模式的更多相关文章

  1. JS常用的设计模式(14)—— 备忘录模式

    备忘录模式在js中经常用于数据缓存. 比如一个分页控件, 从服务器获得某一页的数据后可以存入缓存.以后再翻回这一页的时候,可以直接使用缓存里的数据而无需再次请求服务器. 实现比较简单,伪代码: var ...

  2. js设计模式——7.备忘录模式

    js设计模式——7.备忘录模式 /*js设计模式——备忘录模式*/ // 备忘类 class Memento { constructor(content) { this.content = conte ...

  3. 再起航,我的学习笔记之JavaScript设计模式24(备忘录模式)

    备忘录模式 概念介绍 备忘录模式(Memento): 在不破坏对象的封装性的前提下,在对象之外捕获并保存该对象内部的状态以便日后对象使用或者对象恢复到以前的某个状态. 简易分页 在一般情况下我们需要做 ...

  4. MementoPattern(备忘录模式)

    /** * 备忘录模式 * @author TMAC-J * 用于存储bean的状态 */ public class MementoPattern { public class Memento{ pr ...

  5. C#设计模式-备忘录模式

    访问者模式的实现是把作用于某种数据结构上的操作封装到访问者中,使得操作和数据结构隔离.而本文要介绍的备忘者模式与命令模式有点相似,不同的是,命令模式保存的是发起人的具体命令(命令对应的是行为),而备忘 ...

  6. C#设计模式系列:备忘录模式(Memento)

    1.备忘录模式简介 1.1>.定义 备忘录模式在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 1.2>.使用频率 ...

  7. php实现设计模式之 备忘录模式

    <?php /*备忘录模式:在不破坏封装的前提下,获取对象的内部状态,并且在对象外保存该状态.这样就可以将该对象恢复到保存之前的状态(行为模式) * * 发起人:记录当前时刻的内部状态,负责定义 ...

  8. java设计模式之备忘录模式

    备忘录模式 备忘录模式是一种软件设计模式:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态.一听到备忘录这个字的时候想起了小小时打的游 ...

  9. 备忘录模式(Memento Pattern)

    在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原 ...

随机推荐

  1. JAVA的Hashtable在遍历时的迭代器线程问题

    这篇博客主要讲什么 Hashtable及其内部类的部分源码分析 Hashtable在遍历时的java.util.ConcurrentModificationException异常的来由和解决 单机在内 ...

  2. C#设计模式-单实例

    单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...

  3. 任务05—学习 MARKDOWN 语言

    我的简历地址: https://github.com/jinxiaohang/MyResume/blob/master/ForJavaJob.md 本任务主要目的掌握markdown. 1.首先是工具 ...

  4. JavaScript确定一个字符串是否包含在另一个字符串中的四种方法

    一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...

  5. centos安装lumen

    刚开始安装报错,我用的是php7,先安装zip,uzip扩展 yum install zip unzip php7.0-zip 然后通过 Composer 的 create-project 命令来安装 ...

  6. 学习Hive和Impala必看经典解析

    Hive和Impala作为数据查询工具,它们是怎样来查询数据的呢?与Impala和Hive进行交互,我们有哪些工具可以使用呢? 我们首先明确Hive和Impala分别提供了对应查询的接口: (1)命令 ...

  7. (4.5)DBCC的概念与用法(DBCC TRACEON、DBCC IND、DBCC PAGE)

    转自:http://www.cnblogs.com/huangxincheng/p/4249248.html DBCC的概念与用法 一:DBCC 1:什么是DBCC 我不是教学老师,我也说不到没有任何 ...

  8. 教你管理SQL实例系列(1-15)

    全系列转自:51CTO ->jimshu http://jimshu.blog.51cto.com 目录及原本连接如下: 教你管理SQL实例(1)数据库实例 教你管理SQL实例(2)服务启动帐户 ...

  9. MySQL学习思维导图

    结束:分享在线下载地址 https://www.xmind.net/m/7t6U/

  10. 随心所欲移动Panel

    C# Winform编程时,有时需要在程序执行时,使窗体中的panel控件可以随意的移动,这时可以采用下面这种方法: 主要包括以下两步: @1:给panel(此处以 RealGLPanel为例说明)添 ...