设计模式之备忘录模式

在日常开发中,我们经常会遇到这样的场景:需要保存对象的某个历史状态,以便将来恢复。这种需求最常见的例子就是"撤销操作"。在这种情况下,**备忘录模式(Memento Pattern)**就派上了用场。

目录

[1. 概念](#1. 概念)

[2. 代码实现](#2. 代码实现)

[3. 总结](#3. 总结)


1. 概念

备忘录模式属于行为型设计模式。它的核心思想是:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将对象恢复到原先保存的状态。

通俗点讲,备忘录模式就像是在玩游戏时存档:你保存了一个状态,之后可以随时"读档"回来。

备忘录模式包含三个关键角色:

  1. Originator(发起人):需要保存状态的对象

  2. Memento(备忘录):用来存储对象的内部状态

  3. Caretaker(管理者):负责保存备忘录,但不会操作或检查其内容

2. 代码实现

这里我将采用一个编辑/撤销文本的案例来实现备忘录模式。

首先我准备一个Editor类

java 复制代码
public class Editor {
    private String text;

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    // 保存当前状态
    public Memento save() {
        return new Memento(text);
    }

    // 恢复状态
    public void restore(Memento memento) {
        this.text = memento.getText();
    }

    // 内部备忘录类
    public static class Memento {
        private final String text;

        private Memento(String text) {
            this.text = text;
        }

        private String getText() {
            return text;
        }
    }
}

这里我定义了一个静态内部类即Memento类,主要作为一个备忘录,也是被记录的状态类。

接下来再定义管理者,负责管理备忘录:

java 复制代码
import java.util.Stack;

public class History {
    private final Stack<Editor.Memento> history = new Stack<>();

    public void push(Editor.Memento memento) {
        history.push(memento);
    }

    public Editor.Memento pop() {
        if (!history.isEmpty()) {
            return history.pop();
        }
        return null;
    }
}

这里我们这个管理类也基于栈来实现,其后进先出的特性可以很好的实现我们需要的写入撤销的业务逻辑。

最后完成一个主测试类:

java 复制代码
public class MementoDemo {
    public static void main(String[] args) {
        Editor editor = new Editor();
        History history = new History();

        editor.setText("Version 1");
        history.push(editor.save());

        editor.setText("Version 2");
        history.push(editor.save());

        editor.setText("Version 3");
        System.out.println("当前内容: " + editor.getText()); // 输出 Version 3

        editor.restore(history.pop());
        System.out.println("撤销后: " + editor.getText()); // 输出 Version 2

        editor.restore(history.pop());
        System.out.println("再次撤销: " + editor.getText()); // 输出 Version 1
    }
}

3. 总结

备忘录模式的封装良好,但其实在实际工作中用到的地方并没有很多,绝大多数的游戏服务器实现存档的功能类似备忘录模式,但是对于后端服务器业务开发过程中,此设计模式的实践并不是很多,大家可以作为了解即可。

相关推荐
这是谁的博客?8 小时前
微服务架构设计模式深度解析:从拆分策略到容灾机制
微服务·设计模式·云原生·架构·架构设计·后端开发·分布式系统
fan_music11 小时前
设计模式学习
c++·设计模式
乐观的山里娃13 小时前
【后编码时代 06】Vibe Coding + Superpowers 完全不够
设计模式·软件工程·ai编程
likerhood14 小时前
设计模式 · 责任链模式(Chain of Responsibility Pattern)
设计模式·责任链模式
追烽少年x16 小时前
STL中的设计模式(一)
c++·设计模式
乐观的山里娃17 小时前
【设计模式 10】抽象工厂:整体换季
设计模式
贵慜_Derek19 小时前
《从零实现 Agent 系统》连载 08|编排与工作流:从 Chat 到任务图
人工智能·设计模式·架构
W.W.H.19 小时前
C++ 设计模式:6 个常用模式的实战示例
开发语言·c++·设计模式
老码观察20 小时前
设计模式实战解读(三):模板方法模式——骨架复用与扩展点设计
设计模式·模板方法模式
雪度娃娃20 小时前
行为型设计模式——状态模式
ui·设计模式·状态模式