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

Java 设计模式之备忘录模式

文章目录

  • Java 设计模式之备忘录模式
    • 概述
    • UML
    • 代码实现

Java 设计模式之备忘录模式

概述

  • 备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。方便对该对象恢复到原先保存的状态。

UML

在这里插入图片描述

  • Originnator:发起者类,指需要被保存状态的对象,负责创建备忘录并恢复状态。
  • Memento:备忘录类,负责存储发起者的内部状态。
  • Caretaker:管理者类,负责保存和管理备忘录,不直接操作备忘录的内容。

代码实现

定义发起者类:

public class TextEditor {
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    // 创建备忘录
    public TextEditorMemento createMemento() {
        return new TextEditorMemento(content);
    }

    // 恢复备忘录
    public void restoreMemento(TextEditorMemento memento) {
        this.content = memento.getContent();
    }

    @Override
    public String toString() {
        return "TextEditor{" +
                "content='" + content + '\'' +
                '}';
    }
}

定义备忘录类:

public class TextEditorMemento {
    private final String content;

    public TextEditorMemento(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }
}

定义管理类:

public class TextEditorHistory {
    private final Stack<TextEditorMemento> history = new Stack<>();

    // 保存状态
    public void save(TextEditor editor) {
        history.push(editor.createMemento());
    }

    // 撤销操作
    public void undo(TextEditor editor) {
        if (!history.isEmpty()) {
            editor.restoreMemento(history.pop());
        } else {
            System.out.println("没有历史数据,不能撤销");
        }
    }
}

使用:

public class Client {
    public static void main(String[] args) {
        TextEditorHistory history = new TextEditorHistory();
        TextEditor editor = new TextEditor();

        // 编辑内容并保存状态
        editor.setContent("one");
        history.save(editor);
        System.out.println(editor);

        // 编辑内容并保存状态
        editor.setContent("two");
        history.save(editor);
        System.out.println(editor);

        // 只编辑内容
        editor.setContent("three");

        // 第1次撤销操作
        history.undo(editor);
        System.out.println("第1次撤销操作后:" + editor);

        // 第2次撤销操作
        history.undo(editor);
        System.out.println("第2次撤销操作后:" + editor);

        // 第3次撤销操作
        history.undo(editor);
        System.out.println("第3次撤销操作后:" + editor);
    }
}

输出:

TextEditor{content='one'}
TextEditor{content='two'}
第1次撤销操作后:TextEditor{content='two'}
第2次撤销操作后:TextEditor{content='one'}
没有历史数据,不能撤销
第3次撤销操作后:TextEditor{content='one'}

相关文章:

  • 2.11学习
  • Unity 命令行设置运行在指定的显卡上
  • Hyper-V管理器连接服务器提示你没有完成此任务所需的权限
  • 日常故障排查 - Linux常用命令
  • C++ 中的继承与派生
  • Windchill 成套的解决方案
  • Linux Mem -- ARM8.5-A Memory Tagging Extension
  • 人工智能大模型技术剖析:分类、对比与性能洞察
  • 【情感识别】SECap: Speech Emotion Captioning with Large Language Model 论文阅读
  • 逻辑回归不能解决非线性问题,而svm可以解决
  • 【prompt实战】旅行攻略顾问
  • 二、k8s项目的生命周期
  • 闭源大语言模型的怎么增强:提示工程 检索增强生成 智能体
  • hot100_101. 对称二叉树
  • 国家队出手!DeepSeek上线国家超算互联网平台!
  • 使用docker compose启动postgres并设置时区
  • 力扣100. 相同的树(利用分解思想解决)
  • 相机与激光雷达联合标定综述
  • 网络安全 | 什么是网络安全?
  • Vue2/Vue3生命周期对比
  • 中美经贸高层会谈将在午餐后继续
  • 印巴开始互袭军事目标,专家:冲突“螺旋式升级”后果或不可控
  • “毛茸茸”的画,诗意、温暖又治愈
  • 【社论】职业上新,勇于“尝新”
  • 1450亿元!财政部拟发行2025年中央金融机构注资特别国债(二期)
  • 河南省省长王凯在郑州调研促消费工作,走访蜜雪冰城总部