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.备忘录模式的更多相关文章

  1. JS设计模式——5.单体模式

    JS设计模式——5.单体模式 http://www.cnblogs.com/JChen666/p/3610585.html   单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? ...

  2. 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

    原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...

  3. 折腾Java设计模式之备忘录模式

    原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...

  4. C#设计模式:备忘录模式(Memento Pattern)

    一,C#设计模式:备忘录模式(Memento Pattern) 1.发起人角色(Originator):记录当前时刻的内部状态,负责创建和恢复备忘录数据.负责创建一个备忘录Memento,用以记录当前 ...

  5. js设计模式——6.模板方法模式与职责链模式

    js设计模式——6.模板方法模式与职责链模式 职责链模式

  6. js设计模式——5.状态模式

    js设计模式——5.状态模式 代码演示 /*js设计模式——状态模式*/ // 状态(红灯,黄灯,绿灯) class State { constructor(color) { this.color = ...

  7. js设计模式——4.迭代器模式

    js设计模式——4.迭代器模式 代码演示 /*js设计模式——迭代器模式*/ class Iterator { constructor(container) { this.list = contain ...

  8. js设计模式——2.外观模式

    js设计模式——2.外观模式

  9. js设计模式——1.代理模式

    js设计模式——1.代理模式 以下是代码示例 /*js设计模式——代理模式*/ class ReadImg { constructor(fileName) { this.fileName = file ...

随机推荐

  1. 开源实践分享:Ceph bluestore部署实践

    https://blog.51cto.com/99cloud/2119884 Ceph bluestore部署 首先为大家分享Ceph bluestore具体该如何部署,使用环境如下• 单节点• Ce ...

  2. css float 浮动

    CSS Float(浮动) 什么是 CSS Float(浮动)?大理石平台价格 CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. Float(浮动),往往是用于图像, ...

  3. 容器————priority_queue

    #include <queue> 与queue不同的是可以自定义其中数据的优先级,让优先级高的先出队列. 优先队列具有队列的所有特性,包括基本操作,只是在这基础上添加了内部的一个排序,它本 ...

  4. 团队冲刺DAY6

    团队冲刺DAY6 今天的内容是无图形界面的客户端和服务器的加密解密系统. 通信时用的socket方法,内置的密钥,端口,ip地址. 客户端: import java.io.*; import java ...

  5. 慎用margin系列1---CSS的margin塌陷(collapse) 问题与对策

      对于以下简单代码: 如果您认为应该是这样的话: 那就错了.结果是这样的: 因为CSS中存在一个margin collapse,即边界塌陷或者说边界重叠.对于上下两个并列的div块而言,上面div的 ...

  6. DB2连接

    ibm_db.connect 创建非持久连接. ibm_db.pconnect 创建持久连接. 在最初的Python脚本请求之后,持久的连接保持打开状态,这允许后续的Python请求重新使用连接. 后 ...

  7. 关于document.body.scrollTop用法

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  8. pthread_create()的一个错误示例

    //pthread_create()函数的错误示例 //新建线程同时传入线程号.线程号总和和消息 #include <stdio.h> #include <pthread.h> ...

  9. 树莓派上Opencv highgui的问题

    错误描述:https://bbs.csdn.net/topics/394616975?page=1#post-409508178 解决方案:直接改系统环境变量 # vim /etc/profile e ...

  10. mac 完全卸载vscode

    原文分隔线====================== while writing go this morning, I found that the wrong code are not under ...