js设计模式——7.备忘录模式
js设计模式——7.备忘录模式
  
/*js设计模式——备忘录模式*/ // 备忘类
class Memento {
constructor(content) {
this.content = content;
} getContent() {
return this.content;
}
} // 备忘列表
class CarTaker {
constructor() {
this.list = [];
} add(memento) {
this.list.push(memento);
} get(index) {
return this.list[index];
} getList() {
return this.list
}
} // 编辑器
class Editor {
constructor() {
this.content = null;
} setContent(content) {
this.content = content;
} getContent() {
return this.content;
} saveContentToMemento() {
return new Memento(this.content);
} getConentFromMemento(memento) {
this.content = memento.getContent();
}
} // 测试代码
let editor = new Editor()
let careTaker = new CarTaker() editor.setContent('111')
editor.setContent('222') careTaker.add(editor.saveContentToMemento()) // 将当前222内容备份
editor.setContent('333')
careTaker.add(editor.saveContentToMemento()) // 将当前333内容备份
editor.setContent('444') console.log(editor.getContent())
editor.getConentFromMemento(careTaker.get(1)) // 撤销
console.log(editor.getContent())
editor.getConentFromMemento(careTaker.get(0)) // 撤销
console.log(editor.getContent())

js设计模式——7.备忘录模式的更多相关文章
- JS设计模式——5.单体模式
		
JS设计模式——5.单体模式 http://www.cnblogs.com/JChen666/p/3610585.html 单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? ...
 - 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
		
原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...
 - 折腾Java设计模式之备忘录模式
		
原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...
 - C#设计模式:备忘录模式(Memento Pattern)
		
一,C#设计模式:备忘录模式(Memento Pattern) 1.发起人角色(Originator):记录当前时刻的内部状态,负责创建和恢复备忘录数据.负责创建一个备忘录Memento,用以记录当前 ...
 - js设计模式——6.模板方法模式与职责链模式
		
js设计模式——6.模板方法模式与职责链模式 职责链模式
 - js设计模式——5.状态模式
		
js设计模式——5.状态模式 代码演示 /*js设计模式——状态模式*/ // 状态(红灯,黄灯,绿灯) class State { constructor(color) { this.color = ...
 - js设计模式——4.迭代器模式
		
js设计模式——4.迭代器模式 代码演示 /*js设计模式——迭代器模式*/ class Iterator { constructor(container) { this.list = contain ...
 - js设计模式——2.外观模式
		
js设计模式——2.外观模式
 - js设计模式——1.代理模式
		
js设计模式——1.代理模式 以下是代码示例 /*js设计模式——代理模式*/ class ReadImg { constructor(fileName) { this.fileName = file ...
 
随机推荐
- .net从服务端下载文件(可以断点续传)
			
public void DownFile(string guid) { var fileTransfer = new FileTransfer(); var directoryPath = Path. ...
 - POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)
			
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N < ...
 - 前端每日实战:58# 视频演示如何用纯 CSS 创作一只卡通鹦鹉
			
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vrRmWy 可交互视频 此视频是可 ...
 - oracle 批处理 bulk collect 带来的性能优势
			
create table -- drop table tmp_20190706_220000-- truncate table tmp_20190706_220000 create table tmp ...
 - 自定义缓存管理器 或者 Spring -- cache
			
Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...
 - ceph命令拷屏
			
常用命令ceph -w ceph df ceph features ceph fs ls ceph fs status ceph fsid ceph health ceph -s ceph statu ...
 - rsync+inotify实现实时同步案例
			
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
 - PowerDesigner小技巧(整理中)
			
1.在修改name的时候,code的值将跟着联动 修改方法:PowerDesign中的选项菜单里修改,在[Tool]-->[General Options]->[Dialog]->[ ...
 - Spring 相关目录
			
Spring 相关目录 学习笔记 Spring 学习笔记 IoC 基础 Spring 学习笔记 Resource 资源 Spring 学习笔记 数据绑定,校验,BeanWrapper 与属性编辑器 源 ...
 - Sqlplus常用指令
			
一.ORACLE的启动和关闭1.在单机环境下2.在双机环境下二.Oracle数据库有哪几种启动方式1.startup nomount 非安装启动,这种方式启动下可执行:重建控制文件.重建数据库2.st ...