zoukankan      html  css  js  c++  java
  • 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())
    

     

  • 相关阅读:
    linux下创建一个指定大小的文件
    批量替换多个文件中的字符串
    redhat 搭建yum 源
    python ConfigParser 模块
    python yaml 模块
    python xml文件处理
    py2exe 和pyinstaller打包
    wxpython 学习之 --threading
    wxpython 学习之 --文本框与Boxsizer布局管理器
    wxpython 学习之 --窗口分割
  • 原文地址:https://www.cnblogs.com/hpx2020/p/10730761.html
Copyright © 2011-2022 走看看