备忘录模式通过发起者保存状态至备忘录、管理者存储备忘录、发起者从中恢复来实现状态回滚,适用于撤销功能与快照保存。

在Go语言中,备忘录模式(Memento Pattern)用于保存和恢复对象的内部状态,同时不破坏封装性。它适用于需要实现撤销功能、快照保存或状态回滚的场景。通过将状态保存到一个“备忘录”对象中,原始对象(发起者)可以随时从备忘录中恢复之前的状态,而外部对象(管理者)只能持有备忘录,不能访问其内容。
备忘录模式通常包含三个核心角色:
以下是一个简单的文本编辑器使用备忘录模式保存历史状态的例子:
// 发起者:文本编辑器 type Editor struct { text string }
func (e *Editor) SetText(text string) { e.text = text }
func (e *Editor) GetText() string { return e.text }
// 创建备忘录 func (e Editor) Save() Memento { return &Memento{text: e.text} }
// 从备忘录恢复状态 func (e Editor) Restore(m Memento) { e.text = m.text }
// 备忘录:仅保存状态,不暴露操作方法 type Memento struct { text string }
// 管理者:保存多个备忘录(如历史记录) type History struct { mementos []*Memento }
func (h History) Push(m Memento) { h.mementos = append(h.mementos, m) }
func (h History) Pop() Memento { if len(h.mementos) == 0 { return nil } index := len(h.mementos) - 1 m := h.mementos[index] h.mementos = h.mementos[:index] return m }
使用方式:
立即学习“go语言免费学习笔记(深入)”;
func main() { editor := &Editor{} history := &History{}editor.SetText("第一版内容")
history.Push(editor.Save()) // 保存状态
editor.SetText("第二版内容")
history.Push(editor.Save())
editor.SetText("第三版内容")
// 撤销一次
if m := history.Pop(); m != nil {
editor.Restore(m)
}
fmt.Println(editor.GetText()) // 输出:第二版内容}
Go语言没有严格的访问控制(如private关键字),因此需通过命名约定和包隔离来实现封装。建议将Memento定义为不导出类型(小写开头),并放在独立包中,避免外部直接访问其字段。
备忘录模式适合需要频繁撤销、重做或保存快照的功能,比如编辑器、游戏存档、事务回滚等。
基本上就这些,用好这个模式能让状态管理更清晰安全。
以上就是Golang如何使用备忘录模式保存对象状态的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号