首页 > 后端开发 > Golang > 正文

Golang命令模式撤销与重做操作示例

P粉602998670
发布: 2025-10-08 19:24:02
原创
238人浏览过
通过命令模式实现文本编辑器的撤销与重做功能,定义Command接口包含Execute和Undo方法,InsertCommand实现插入与删除操作,TextEditor作为接收者处理文本内容,CommandManager维护历史栈和重做栈,执行命令时记录到历史栈,撤销时移除并加入重做栈,重做时重新执行并返回历史栈,支持操作回溯与恢复。

golang命令模式撤销与重做操作示例

在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)
登录后复制

}

造物云营销设计
造物云营销设计

造物云是一个在线3D营销设计平台,0基础也能做电商设计

造物云营销设计 37
查看详情 造物云营销设计

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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号