通过命令模式实现文本编辑器的撤销与重做功能,定义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 + rightfmt.Printf("插入 '%s',当前内容: %sn", 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 + rightfmt.Printf("删除 %d 字符,当前内容: %sn", 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)
}
Shakker
多功能AI图像生成和编辑平台
103 查看详情
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 接口即可。
基本上就这些。命令模式结合历史栈,让撤销重做变得清晰可控。
以上就是Golang命令模式撤销与重做操作示例的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1133849.html
微信扫一扫
支付宝扫一扫