通过命令模式实现文本编辑器的撤销与重做功能,定义Command接口包含Execute和Undo方法,InsertCommand实现插入与删除操作,TextEditor作为接收者处理文本内容,CommandManager维护历史栈和重做栈,执行命令时记录到历史栈,撤销时移除并加入重做栈,重做时重新执行并返回历史栈,支持操作回溯与恢复。

在Go语言中实现命令模式的撤销与重做功能,能有效解耦操作请求者与执行者,同时支持操作的历史管理。下面通过一个简单的文本编辑器示例,展示如何用命令模式实现“插入文本”的撤销与重做。
定义命令接口
所有可撤销、可重做的命令都应实现统一接口,包含执行、撤销两个方法:
type Command interface {
Execute()
Undo()
}
实现具体命令:插入文本
InsertCommand 记录插入的位置和内容,以便后续撤销:
type InsertCommand struct {
editor *TextEditor
text string
pos int
}
func (c *InsertCommand) Execute() {
c.editor.Insert(c.text, c.pos)
}
func (c *InsertCommand) Undo() {
c.editor.Delete(c.pos, len(c.text))
}
文本编辑器:接收者角色
TextEditor 是实际处理文本的对象,提供插入和删除方法:
立即学习“go语言免费学习笔记(深入)”;
type TextEditor struct {
content string
}
func (e *TextEditor) Insert(text string, pos int) {
if pos > len(e.content) {
pos = len(e.content)
}
left := e.content[:pos]
right := e.content[pos:]
e.content = left + text + right
fmt.Printf("插入 '%s',当前内容: %s\n", text, e.content)
}
func (e *TextEditor) Delete(pos, length int) {
if pos+length > len(e.content) {
length = len(e.content) - pos
}
left := e.content[:pos]
right := e.content[pos+length:]
e.content = left + right
fmt.Printf("删除 %d 字符,当前内容: %s\n", length, e.content)
}
命令管理器:支持撤销与重做
CommandManager 维护命令历史,支持撤销和重做:
type CommandManager struct {
history []Command
undone []Command // 存储已撤销的命令,用于重做
}
func (m *CommandManager) ExecuteCommand(cmd Command) {
cmd.Execute()
m.history = append(m.history, cmd)
m.undone = nil // 执行新命令后,清空重做栈
}
func (m *CommandManager) Undo() {
if len(m.history) == 0 {
fmt.Println("无可撤销的操作")
return
}
last := m.history[len(m.history)-1]
m.history = m.history[:len(m.history)-1]
last.Undo()
m.undone = append(m.undone, last)
}
func (m *CommandManager) Redo() {
if len(m.undone) == 0 {
fmt.Println("无可重做的操作")
return
}
last := m.undone[len(m.undone)-1]
m.undone = m.undone[:len(m.undone)-1]
last.Execute()
m.history = append(m.history, last)
}
使用示例
组合各组件进行测试:
func main() {
editor := &TextEditor{content: ""}
manager := &CommandManager{}
cmd1 := &InsertCommand{editor: editor, text: "Hello", pos: 0}
cmd2 := &InsertCommand{editor: editor, text: " World", pos: 5}
manager.ExecuteCommand(cmd1)
manager.ExecuteCommand(cmd2)
manager.Undo() // 撤销 " World"
manager.Undo() // 撤销 "Hello"
manager.Redo() // 重做 "Hello"
manager.Redo() // 重做 " World"}
输出结果会清晰展示每次操作、撤销和重做的过程。该结构易于扩展,比如添加“删除命令”或“格式化命令”,只需实现 Command 接口即可。
基本上就这些。命令模式结合历史栈,让撤销重做变得清晰可控。










