当前位置: 首页 > news >正文

【设计模式】GoF设计模式之备忘录模式(Memento Pattern)

设计模式之备忘录模式 Memento Pattern

    • V1.0
    • 核心概念
    • 角色
    • 代码示例
      • 程序运行结果
      • 代码讲解
    • 适用场景

V1.0

核心概念

备忘录模式的核心是定义一个备忘录类(Memento),这个类的实例能够表示发起人类(Originator)的一种状态。发起人类提供生成备忘录对象的接口,以保存其当时的状态,也提供了接口传入备忘录实例,能够使用备忘录对象实例中保存的信息恢复其保存时的状态。

角色

  • 发起人(Originator):具体的业务类,提供生成备忘录的接口,也提供使用备忘录对象来恢复状态的接口。
  • 备忘录(Memento):由发起人根据其状态创建,保存了发起人创建时的状态信息,之后可以用来恢复发起人的状态。
  • 管理者(Caretaker):用于保存备忘录对象,以及在向其查询时提供备忘录对象。

代码示例

using System;
using System.ComponentModel;public class Originator
{private string _state = null;public string state{get => _state;set => _state = value;}public Memento createMemento(){return new Memento(state);}public void setState(string state){this.state = state;}public void recoverFromMemento(Memento m){state = m.state;Console.WriteLine($"The state of the originator is restored to '{state}'");}public void displayState(){Console.WriteLine($"The state of the originator now: {state}");}
};public class Memento
{private string _state;public string state{get => _state;set => _state = value;}public Memento(string state){this.state = state;}};public class Caretaker
{private List<Memento> list = new();public void addMemento(Memento m){list.Add(m);}public Memento getMemento(int index){return list.Count > index ? list[index] : null;}
}public class Program
{public static void Main(string[] arg){//创建发起人和管理者Originator o = new();Caretaker ct = new();//设置发起人的状态,展示并保存为Memento对象到管理者o.setState("Idle");o.displayState();ct.addMemento(o.createMemento());//设置发起人的状态,展示并保存为Memento对象到管理者o.setState("Working");o.displayState();ct.addMemento(o.createMemento());//设置发起人的状态,展示并保存为Memento对象到管理者o.setState("Stop");o.displayState();ct.addMemento(o.createMemento());//恢复Originator的状态到Idle状态o.recoverFromMemento(ct.getMemento(0));o.displayState();//恢复Originator的状态到Working状态o.recoverFromMemento(ct.getMemento(1));o.displayState();}
}

程序运行结果

The state of the originator now: Idle
The state of the originator now: Working
The state of the originator now: Stop
The state of the originator is restored to 'Idle'
The state of the originator now: Idle
The state of the originator is restored to 'Working'
The state of the originator now: Working

代码讲解

  • Originator类
    在这段代码中,Originator类的state属性是Originator类的状态,Originator类也提供了创建备忘录对象的createMemento方法,从备忘录对象中恢复状态的recoverFromMemento方法,以及展示Originator类当前状态的方法displayState。

  • Memento类
    Memento类中的state属性用来保存对应的Originator的状态,在Originator恢复状态时要用到。

  • Caretaker类
    Caretaker类定义了private访问权限的list列表,用于保存Memento对象,提供了addMemento方法用来向list列表中新增Memento对象,以及getMemento方法用于从list列表中获取指定序号的Memento对象。

  • Program类
    Main函数中创建了发起人和管理者,变更发起人的状态,以及展示并保存Memento对象,之后从管理者中获取Memento对象,以恢复发起人的状态。

适用场景

备忘录模式可以用来保存快照的场景中,例如程序或者虚拟机的快照。
备忘录模式也可以用在撤销或恢复状态的场景中,通过保存当前的状态,在撤消操作时可以使用备忘录对象恢复之前的状态。

http://www.xdnf.cn/news/245593.html

相关文章:

  • 文件操作--文件包含漏洞
  • 【IP101】图像滤波技术详解:从均值滤波到高斯滤波的完整指南
  • 【QNX+Android虚拟化方案】137 - msm-5.4 Kernel U盘 插入中断、枚举、匹配完整流程详解
  • 深度学习框架:PyTorch使用教程 !!
  • 缓存:缓解读库压力的高效方案与应用实践
  • DeepSeek V3 架构创新:大规模MoE与辅助损失移除
  • 本文不定期更新,用于收录各种怪异的python脚本
  • 实现Sentinel与Nacos的规则双向同步
  • Java朴实无华按天计划从入门到实战(94天直达Java高阶)
  • [计算机科学#7]:CPU的三阶段,取指令、解码、执行
  • 时序建模演进之路:从 MLP、RNN 到 LSTM 与 GRU
  • 【Linux】Makefile
  • 小结:ipsec-ike
  • 例数据中关键指标对应的SQL查询模板
  • mysql数据库备份与恢复方法
  • Java学习手册:Spring 事务管理
  • 面试的各种类型
  • Linux日常使用与运维的AI工具全景调研:效率革命的终极指南
  • (A题|支路车流量推测问题)2025年第二十二届五一数学建模竞赛(五一杯/五一赛)解题思路|完整代码论文集合
  • 【Dify系列教程重置精品版】第五章:Dify配置Ollama
  • C++漫溯键值的长河:map set
  • ES6-Set-Map对象小记
  • 业务流程BPM能力框架体系及华为中兴流程变革案例P83(83页PPT)(文末有下载方式)
  • 信息系统项目管理师-软考高级(软考高项)​​​​​​​​​​​2025最新(六)
  • 初识 Java
  • OpenGL-ES 学习(12) ---- VBO EBO VAO
  • 网络分析/
  • 前端如何转后端
  • 【Docker】Dockerfile 使用
  • 【AI大模型学习路线】第一阶段之大模型开发基础——第二章(大模型的训练与应用)大模型发展史?大模型预训练、微调到应用的过程?