首页 > 其他分享 >[Design Pattern] Memento Pattern

[Design Pattern] Memento Pattern

时间:2024-08-19 14:51:40浏览次数:6  
标签:const TodoList Pattern .# Design text array data Memento

// memento.js

import { TodoList } from "./classes.js";

export const TodoHistory = {
  history: [],
  push(state) {
    if (state) {
      // always push a new Set to avoid reference issues
      this.history.push(new Set([...state]));
    }
  },
  pop() {
    if (this.history.length > 1) {
      this.history.pop();
      return this.history.pop();
    }
  },
};

// Push new state into history when the list changes
const todoList = TodoList.getInstance();
todoList.addObserver(() => {
  TodoHistory.push(todoList.items);
});

We store the whole list into history array, when store the list, we need to save the copy to avoid reference issue.

 

Observer mixin:

export const observerMixin = {
  observers: new Set(),
  addObserver(obs) {
    this.observers.add(obs);
  },
  removeObserver(obs) {
    this.observers.delete(obs);
  },
  notify() {
    this.observers.forEach((obs) => obs());
  },
};

 

TodoList:

import { observerMixin } from "./mixin.js";

export class TodoItem {
  constructor(text) {
    this.text = text;
  }

  equals(other) {
    return this.text === other.text;
  }
}

class TodoList {
  #data = new Set();

  get items() {
    return this.#data;
  }

  /**
   * Singleton instance
   */
  static instance = null;
  static {
    this.instance = new TodoList();
  }
  static getInstance() {
    return this.instance;
  }

  constructor() {
    if (TodoList.instance) {
      throw new Error("Use TodoList.getInstance() to access the list");
    }
  }

  // behavior
  add(item) {
    const array = Array.from(this.#data);
    const todoExists = array.filter((t) => t.equals(item)).length > 0;
    if (!todoExists) {
      this.#data.add(item);
      this.notify();
    }
  }

  delete(text) {
    const array = Array.from(this.#data);
    const todoToDelete = array.filter((t) => t.text === text)[0];
    if (todoToDelete) {
      this.#data.delete(todoToDelete);
      this.notify();
    }
  }

  find(text) {
    const array = Array.from(this.#data);
    return array.find((t) => t.text === text);
  }

  replaceList(list) {
    this.#data = list;
    this.notify();
  }
}

/**Mixin */
Object.assign(TodoList.prototype, observerMixin);

export { TodoList };

 

标签:const,TodoList,Pattern,.#,Design,text,array,data,Memento
From: https://www.cnblogs.com/Answer1215/p/18367285

相关文章

  • Twenty Lectures on Algorithmic Game Theory 算法博弈论二十讲 Lecture 2 Mechanism
    TwentyLecturesonAlgorithmicGameTheory算法博弈论二十讲Lecture2MechanismDesignBasics过去的15年里,计算机科学与经济学之间进行了活跃的互动,催生了算法博弈论这一新兴领域。许多现代计算机科学中的核心问题,从大规模网络中的资源分配到在线广告,都涉及多个自......
  • 设计模式---构建者模式(Builder Pattern)
    构建者模式(BuilderPattern)是一种创建型设计模式,旨在将复杂对象的构建过程与其表示分离。它允许使用相同的构建过程创建不同的表示。该模式通常用于构建复杂对象,这些对象由多个部分组成或具有多个可选属性。构建者模式的核心要素:Builder(构建者):定义构建对象的接口,声明创建部......
  • Python - Structural Design Patterns
    •TheadapterpatternTheadapterpatternisastructuraldesignpatternthathelpsusmaketwoincompatibleinterfaces compatible.Whatdoesthatreallymean?Ifwehaveanoldcomponentandwewanttouseitinanew system,oranewcomponentthatwew......
  • Python - Foundational Design Principles
    EncapsulateWhatVariesOneofthemostcommonchallengesinsoftwaredevelopmentisdealingwithchange.Requirements evolve,technologiesadvance,anduserneedsalsochange.Therefore,itiscrucialtowritecodethat canadaptwithoutcausingaripple......
  • 【公式推导】Elucidating the Design Space of Diffusion-Based Generative Models 【
    ElucidatingtheDesignSpaceofDiffusion-BasedGenerativeModels论文精读关注B站可以观看更多实战教学视频:hallo128的个人空间【更新中】EDM论文精读论文链接(1)论文:ElucidatingtheDesignSpaceofDiffusion-BasedGenerativeModels(2)引用:KarrasT,Aittala......
  • [Design Pattern] Value Object
    ProblemtoSolveReparesentavaluethatisimmutableanddistinctfromotherobjectsbasedonitspropertiesratherthanitsidentity. SolutionCreateaclasswhereinstancesareconsideredequalifalltheirpropertiesareequalsandensuretheobject......
  • Vue3的福音框架:Arco.Design
    Vue3的福音框架:Arco.Design在Vue3逐渐成为前端开发主流技术的今天,开发者们对于高性能、易扩展、且设计美观的UI框架需求日益增长。Arco.Design,作为字节跳动推出的一套企业级UI组件库,正是为满足这些需求而生。本文将从Arco.Design的起源、特点、安装与配置、组件使用、主题......
  • Ant-Design-Vue快速上手指南+排坑
    AntDesignVue是一个基于Vue.js的UI组件库,它提供了丰富的组件和样式,可以帮助我们快速构建美观、高效的前端界面。以下是AntDesignVue的快速上手指南和一些常见问题的排解方法。安装AntDesignVue首先,在你的Vue项目中安装AntDesignVue。使用npm安装可以通过以下命令完......
  • TI 生成 TPG 流程 Test Pattern Generator
    TI生成TPGTestPatternGenerator1.主要作用:生成各种预定义的图形和模式用来检查CSI接口的图像传输质量调试和验证使用TPG生成的测试图形可以方便地验证接口的正确性和稳定性2.代码中的体现staticconstchar*constub960_tpg_qmenu[]={ "Disabled", "1vertical......
  • 设计模式 - Singleton pattern 单例模式
    文章目录定义单例模式的实现构成构成UML图单例模式的六种实现懒汉式-线程不安全懒汉式-线程安全饿汉式-线程安全双重校验锁-线程安全静态内部类实现枚举实现总结其他设计模式文章:最后定义单例模式是一种创建型设计模式,它用来保证一个类只有一个实例,并且提供一个......