Go语言中通过命令模式实现撤销操作的核心是将请求封装为对象。定义包含Execute和Undo方法的Command接口,具体命令如InsertCommand和DeleteCommand保存执行上下文,调用者CommandManager维护命令历史栈,执行时记录,撤销时调用最后命令的Undo并出栈,从而实现可逆操作。

在Go语言中实现命令模式并支持撤销操作,核心是将“请求”封装成独立的对象,使得命令的执行和撤销可以统一管理。通过定义命令接口、具体命令结构体以及调用者(Invoker),我们可以灵活地执行和回退操作。
定义命令接口
命令模式的基础是一个通用的命令接口,包含执行(Execute)和撤销(Undo)两个方法:
type Command interface {
Execute()
Undo()
}这个接口让所有具体命令遵循相同的契约,便于统一调度。
实现具体命令
以文本编辑器中的“插入文本”命令为例,展示如何携带状态以支持撤销:
立即学习“go语言免费学习笔记(深入)”;
type InsertCommand struct {
editor *Editor
text string
}
func (c *InsertCommand) Execute() {
c.editor.Insert(c.text)
}
func (c *InsertCommand) Undo() {
// 删除最后插入的内容
last := len(c.text)
if end := len(c.editor.Content); end >= last {
c.editor.Content = c.editor.Content[:end-last]
}
}
另一个例子是“删除选中内容”的命令,需要保存被删文本以便恢复:
type DeleteCommand struct {
editor *Editor
selection string
}
func (c *DeleteCommand) Execute() {
c.selection = c.editor.GetSelection()
c.editor.ClearSelection()
}
func (c *DeleteCommand) Undo() {
c.editor.Insert(c.selection)
}
关键在于命令对象要保存足够的上下文信息,比如原始数据或操作前的状态。
使用调用者管理命令历史
定义一个命令管理器来维护已执行的命令栈,支持撤销操作:
type CommandManager struct {
history []Command
}
func (m *CommandManager) Execute(command Command) {
command.Execute()
m.history = append(m.history, command)
}
func (m *CommandManager) Undo() {
if len(m.history) == 0 {
return
}
last := len(m.history) - 1
m.history[last].Undo()
m.history = m.history[:last]
}
每次执行命令都记录到历史栈,Undo则弹出最后一个命令并调用其Undo方法。
实际使用示例
整合上述组件进行测试:
type Editor struct {
Content string
}
func (e *Editor) Insert(text string) {
e.Content += text
}
func (e *Editor) GetSelection() string {
// 简化:返回全部内容作为选中部分
return e.Content
}
func (e *Editor) ClearSelection() {
e.Content = ""
}
调用流程:
editor := &Editor{}
manager := &CommandManager{}
cmd1 := &InsertCommand{editor, "Hello"}
manager.Execute(cmd1)
fmt.Println(editor.Content) // Hello
cmd2 := &DeleteCommand{editor, ""}
manager.Execute(cmd2)
fmt.Println(editor.Content) // ""
manager.Undo()
fmt.Println(editor.Content) // Hello
manager.Undo()
fmt.Println(editor.Content) // ""
可以看到内容随着Undo逐步恢复。
基本上就这些。只要每个命令正确保存逆操作所需的数据,就能实现可靠的撤销功能。这种模式也容易扩展重做(Redo)、批量撤销等特性。不复杂但容易忽略细节,比如状态快照的完整性。










