备忘录模式通过发起人、备忘录和管理者三者协作实现对象状态的保存与恢复。发起人Editor保存当前状态到备忘录Memento,管理者History存储多个备忘录以支持撤销操作。示例中编辑器内容和光标位置被依次保存并恢复,体现该模式在Go中实现撤销功能的核心机制。

在Go语言中,备忘录模式(Memento Pattern)可以用来保存和恢复对象的内部状态,同时不破坏封装性。这个模式常用于实现撤销功能、快照机制或事务回滚等场景。核心思想是通过一个“备忘录”对象来存储原对象的状态,之后可由原对象或管理者从备忘录中恢复。
备忘录模式的核心角色
该模式通常包含三个部分:
- 发起人(Originator):需要保存和恢复状态的对象。
- 备忘录(Memento):存储发起人状态的对象,通常只允许发起人访问其内容。
- 管理者(Caretaker):负责保存和提供备忘录,但不能修改或查看其内容。
Go中的实现示例
下面是一个简单的代码示例,演示如何使用备忘录模式保存和恢复结构体状态。
立即学习“go语言免费学习笔记(深入)”;
package mainimport "fmt"
// 发起人:要保存状态的对象 type Editor struct { Content string CursorX int CursorY int }
// 创建备忘录(保存当前状态) func (e Editor) Save() Memento { return &Memento{ Content: e.Content, CursorX: e.CursorX, CursorY: e.CursorY, } }
// 从备忘录恢复状态 func (e Editor) Restore(m Memento) { e.Content = m.Content e.CursorX = m.CursorX e.CursorY = m.CursorY }
// 备忘录:保存状态,对外不可变 type Memento struct { Content string CursorX int CursorY int }
// 管理者:管理多个备忘录(如历史记录) type History struct { states []*Memento }
func (h History) Push(m Memento) { h.states = append(h.states, m) }
func (h History) Pop() Memento { if len(h.states) == 0 { return nil } index := len(h.states) - 1 m := h.states[index] h.states = h.states[:index] return m }
使用方式:保存与恢复
以下是如何使用上述结构进行状态恢复的示例。
立即学习“go语言免费学习笔记(深入)”;
func main() {
editor := &Editor{Content: "Hello", CursorX: 0, CursorY: 0}
history := &History{}
// 保存初始状态
history.Push(editor.Save())
// 修改内容
editor.Content = "Hello World"
editor.CursorX, editor.CursorY = 5, 0
history.Push(editor.Save())
// 再次修改
editor.Content = "Final content"
editor.CursorX, editor.CursorY = 10, 1
fmt.Println("当前内容:", editor.Content) // 输出最新内容
// 撤销一次
m := history.Pop()
if m != nil {
editor.Restore(m)
}
fmt.Println("撤销后内容:", editor.Content)
// 再次撤销
m = history.Pop()
if m != nil {
editor.Restore(m)
}
fmt.Println("再次撤销后内容:", editor.Content)}
输出结果为:
当前内容: Final content
撤销后内容: Hello World
再次撤销后内容: Hello
关键设计要点
在Go中使用备忘录模式时,注意以下几点:
- 备忘录结构体字段应尽量设为私有(小写),并通过方法访问,以增强封装性;本例为了简洁使用了公有字段。
- 可以结合接口进一步解耦,例如定义 Memento 接口,支持多种类型的对象备份。
- 如果状态较大,考虑深拷贝问题,避免引用类型共享导致意外修改。
- History 可限制最大保存数量,防止内存溢出。
基本上就这些。Go虽然没有类和访问修饰符,但通过包级封装和合理结构设计,依然能很好地实现备忘录模式,帮助你在应用中安全地保存和恢复对象状态。










