js备忘录模式
备忘录(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备忘录模式的更多相关文章
- JS常用的设计模式(14)—— 备忘录模式
备忘录模式在js中经常用于数据缓存. 比如一个分页控件, 从服务器获得某一页的数据后可以存入缓存.以后再翻回这一页的时候,可以直接使用缓存里的数据而无需再次请求服务器. 实现比较简单,伪代码: var ...
- js设计模式——7.备忘录模式
js设计模式——7.备忘录模式 /*js设计模式——备忘录模式*/ // 备忘类 class Memento { constructor(content) { this.content = conte ...
- 再起航,我的学习笔记之JavaScript设计模式24(备忘录模式)
备忘录模式 概念介绍 备忘录模式(Memento): 在不破坏对象的封装性的前提下,在对象之外捕获并保存该对象内部的状态以便日后对象使用或者对象恢复到以前的某个状态. 简易分页 在一般情况下我们需要做 ...
- MementoPattern(备忘录模式)
/** * 备忘录模式 * @author TMAC-J * 用于存储bean的状态 */ public class MementoPattern { public class Memento{ pr ...
- C#设计模式-备忘录模式
访问者模式的实现是把作用于某种数据结构上的操作封装到访问者中,使得操作和数据结构隔离.而本文要介绍的备忘者模式与命令模式有点相似,不同的是,命令模式保存的是发起人的具体命令(命令对应的是行为),而备忘 ...
- C#设计模式系列:备忘录模式(Memento)
1.备忘录模式简介 1.1>.定义 备忘录模式在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 1.2>.使用频率 ...
- php实现设计模式之 备忘录模式
<?php /*备忘录模式:在不破坏封装的前提下,获取对象的内部状态,并且在对象外保存该状态.这样就可以将该对象恢复到保存之前的状态(行为模式) * * 发起人:记录当前时刻的内部状态,负责定义 ...
- java设计模式之备忘录模式
备忘录模式 备忘录模式是一种软件设计模式:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态.一听到备忘录这个字的时候想起了小小时打的游 ...
- 备忘录模式(Memento Pattern)
在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原 ...
随机推荐
- TFS二次开发-基线文件管理器(5)-源码文件的读取
在上一节中,我们在保存标签之前,已经将勾选的文件路径保存到了Listbox中,这里只需要将保存的数据输出去为txt文档就可以做版本控制了. 版本文件比较复杂的是如何读取,也就是如何通过文件路径 ...
- java集合的部分接口
接口 Collection<E> public interface Collection<E>extends Iterable<E> Collection 层次结构 ...
- Django中间件,信号,缓存
中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...
- Python多进程multiprocessing
import multiprocessing import time # 具体的处理函数,负责处理单个任务 def func(msg): # for i in range(3): print (msg ...
- Wicket:一种构建和测试动态 Web 页面的简化框架
https://www.ibm.com/developerworks/cn/web/wa-aj-wicket/
- F110 BADI增强
F110*JOB*&------------------------------------------------------------- F110 BADI FI_BSTM_MC_EXI ...
- 分布式计算开源框架Hadoop入门实践(二)
其实参看Hadoop官方文档已经能够很容易配置分布式框架运行环境了,不过这里既然写了就再多写一点,同时有一些细节需要注意的也说明一下,其实也就是这些细节会让人摸索半天.Hadoop可以单机跑,也可以配 ...
- iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期
1. 属性 在开发过程中经常要用到定义属性,@property和@synthesize是经常用到的属性, property在.h文件中作声明,@synthesize在.m文件中用于实现 // Stud ...
- grads 读取shp
自从GrADS2.0.a8版本开始,GrADS引入了对shp图形的支持,关于此格式在这里不多说, 于是今晚就简单测试了一下最简单画图和查询命令(后续还将测试输出shp图形的命令) 测试数据采用的 ...
- Linux Graphic DRI Wayland 显示子系统
转:https://blog.csdn.net/u013165704/article/details/80709547 1. 前言 上篇文章(Linux graphic subsytem(1)_概述) ...