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

在Go语言中实现命令模式的撤销与重做功能,能有效解耦操作请求者与执行者,同时支持操作的历史管理。下面通过一个简单的文本编辑器示例,展示如何用命令模式实现“插入文本”的撤销与重做。
所有可撤销、可重做的命令都应实现统一接口,包含执行、撤销两个方法:
type Command interface {
Execute()
Undo()
}
InsertCommand 记录插入的位置和内容,以便后续撤销:
type InsertCommand struct {
editor *TextEditor
text string
pos int
}
<p>func (c *InsertCommand) Execute() {
c.editor.Insert(c.text, c.pos)
}</p><p>func (c *InsertCommand) Undo() {
c.editor.Delete(c.pos, len(c.text))
}</p>TextEditor 是实际处理文本的对象,提供插入和删除方法:
立即学习“go语言免费学习笔记(深入)”;
type TextEditor struct {
content string
}
<p>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)
}</p><p>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)
}
</font></p><H3>命令管理器:支持撤销与重做</H3><p>CommandManager 维护命令历史,支持撤销和重做:</p><font face="Courier New, Courier, monospace"><pre class="brush:php;toolbar:false;">
type CommandManager struct {
history []Command
undone []Command // 存储已撤销的命令,用于重做
}
<p>func (m *CommandManager) ExecuteCommand(cmd Command) {
cmd.Execute()
m.history = append(m.history, cmd)
m.undone = nil // 执行新命令后,清空重做栈
}</p><p>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]</p><pre class='brush:php;toolbar:false;'>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{}
<pre class='brush:php;toolbar:false;'>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 接口即可。
基本上就这些。命令模式结合历史栈,让撤销重做变得清晰可控。
以上就是Golang命令模式撤销与重做操作示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号