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)
在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原 ...
随机推荐
- MVC5学习系列
前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...
- 巨蟒django之权限10,内容梳理&&权限组件应用
1.CRM项目内容梳理: 2.权限分配 3.权限组件的应用
- SQL Server 2008 收缩日志 清空删除大日志文件
SQL2008 的收缩日志 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消: (SQL2005) BackupLog DNName ...
- Testlink安装访问提示“应用程序DEFAULT WEB SITE”中的服务器错误
错误摘要:HTTP错误403.14 - ForbiddenWeb服务器被配置为不列出此目录的内容.
- win7安装laravel
使用Packagist 镜像 建立一个composer.json文件,内容如下: { "name": "laravel/laravel", "desc ...
- Armijo-Goldstein准则与Wolfe-Powell准则
Armijo-Goldstein准则与Wolfe-Powell准则是不精确的一维搜索的两大准则. 之所以要遵循这些准则是为了能使算法收敛(求最优解).即要使我们的不精确的一维搜索的步长满足一定的规则, ...
- 剑指offer 面试29题
面试29题: 题目:顺时针打印矩阵(同LeetCode 螺旋矩阵打印) 题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 ...
- 简单理解List、set、Map接口之间的联系和区别
联系:Collection与Map属于同等关系,但Map依赖与Collection.Coolection接口的子类包含List(ArrayList.LinkedList等).Set(HashSet.T ...
- Django-Ajax基础知识
Ajax准备知识:json 1.什么是json JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 它基于 ECMAScript (w3c ...
- 面向过程编程实例------grep-rl 'root 路径
#应用:grep -rl 'root' /etc import os def deco(func): def wrapper(*args): g=func(*args) next(g) return ...