备忘录模式在Go中通过小写字段结构体实现封装:原发器(如Editor)提供Save/Restore方法,备忘录(如EditorMemento)为纯只读数据载体,管理者(如History)负责存取快照,确保状态隔离与不可变性。

备忘录模式(Memento Pattern)用于在不破坏封装的前提下,捕获并外部化一个对象的内部状态,以便之后能恢复到原先状态。Golang 没有类和访问修饰符(如 private),但可通过结构体字段首字母小写 + 明确的“备忘录类型”设计,实现语义上的封装与状态隔离。
原发器是需要被保存/恢复状态的对象。它应提供创建备忘录、从备忘录恢复的方法,且内部状态字段保持小写(仅包内可访问)。
例如,一个简单的文本编辑器状态:
type Editor struct {
content string
cursor int
}
func (e *Editor) SetContent(c string) { e.content = c }
func (e *Editor) SetCursor(pos int) { e.cursor = pos }
func (e *Editor) Save() *EditorMemento {
return &EditorMemento{
content: e.content,
cursor: e.cursor,
}
}
func (e *Editor) Restore(m *EditorMemento) {
if m != nil {
e.content = m.content
e.cursor = m.cursor
}
}
备忘录本身不暴露修改能力,仅用于存储快照。字段小写,不提供 setter 方法,也不导出构造函数(避免外部直接 new)。
立即学习“go语言免费学习笔记(深入)”;
关键点:备忘录类型与原发器分离,且无方法——纯粹的数据载体:
type EditorMemento struct {
content string
cursor int
}
// 注意:不导出字段,不提供任何公开方法
// 外部无法修改 m.content 或 m.cursor
这样,即使调用方拿到 *EditorMemento,也无法篡改其内容,保证了状态快照的不可变性。
管理者负责保存和提供备忘录,不关心内部结构。典型场景是支持“撤销”操作的历史栈:
[]*EditorMemento)存档历史editor.Save() 存入备忘录editor.Restore(last)
示例简易管理者:
type History struct {
snapshots []*EditorMemento
}
func (h *History) Push(m *EditorMemento) {
h.snapshots = append(h.snapshots, m)
}
func (h *History) Pop() *EditorMemento {
if len(h.snapshots) == 0 {
return nil
}
last := h.snapshots[len(h.snapshots)-1]
h.snapshots = h.snapshots[:len(h.snapshots)-1]
return last
}
组合三者,模拟一次编辑 → 保存 → 修改 → 撤销流程:
func main() {
editor := &Editor{content: "Hello", cursor: 0}
history := &History{}
// 初始状态保存
history.Push(editor.Save())
// 修改内容
editor.SetContent("Hello, World!")
editor.SetCursor(7)
// 撤销:恢复上一状态
if m := history.Pop(); m != nil {
editor.Restore(m)
}
fmt.Printf("Restored: %q, cursor=%d\n", editor.content, editor.cursor)
// 输出:Restored: "Hello", cursor=0
}
整个过程未暴露 Editor 的字段细节,备忘录数据不可被误改,符合模式初衷。
以上就是如何使用Golang实现备忘录模式_保存对象状态以便恢复的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号