0

0

Golang如何使用命令模式实现撤销功能_Golang Command模式撤销操作实践

P粉602998670

P粉602998670

发布时间:2025-11-29 19:16:02

|

818人浏览过

|

来源于php中文网

原创

命令模式通过封装请求为对象实现撤销功能,Go中定义Command接口含Execute和Undo方法,以TextEditor为接收者处理插入删除操作,InsertCommand和DeleteCommand保存执行上下文并实现正向执行与反向撤销逻辑,CommandHistory维护命令栈管理执行与撤销顺序,客户端按序执行命令并可逐级撤销,支持多步回退且符合开闭原则,便于扩展重做与组合命令。

golang如何使用命令模式实现撤销功能_golang command模式撤销操作实践

在Go语言中,命令模式(Command Pattern)是一种行为设计模式,它将请求封装为对象,从而使你可以用不同的请求对客户进行参数化。使用命令模式实现撤销功能,可以让程序支持操作的回退,比如文本编辑器中的“撤销键入”、“撤销删除”等场景。

命令模式核心结构

命令模式通常包含以下几个角色:

  • Command(命令接口):定义执行和撤销操作的方法。
  • ConcreteCommand(具体命令):实现命令接口,持有接收者对象,并在执行方法中调用接收者的相应操作。
  • Receiver(接收者):真正执行操作的对象。
  • Invoker(调用者):触发命令执行的对象,通常会维护一个命令历史用于撤销。
  • Client(客户端):创建命令对象并绑定接收者。

定义命令接口与基础结构

首先定义一个命令接口,包含Execute()Undo()两个方法:

type Command interface {
    Execute()
    Undo()
}

假设我们要实现一个简单的文本编辑器,支持插入文本和删除文本,并能撤销这些操作。

立即学习go语言免费学习笔记(深入)”;

实现接收者:文本编辑器

接收者是实际处理数据的对象:

type TextEditor struct {
    content string
}

func (t *TextEditor) Insert(text string) { t.content += text }

func (t *TextEditor) Delete(length int) { if length >= len(t.content) { t.content = "" } else { t.content = t.content[:len(t.content)-length] } }

func (t *TextEditor) GetContent() string { return t.content }

实现具体命令

每个操作都封装成一个命令对象,保存执行所需的数据,以便撤销时恢复状态。

插入命令

Transor
Transor

专业的AI翻译工具,支持网页、字幕、PDF、图片实时翻译

下载
type InsertCommand struct {
    editor   *TextEditor
    text     string
}

func NewInsertCommand(editor TextEditor, text string) InsertCommand { return &InsertCommand{editor: editor, text: text} }

func (c *InsertCommand) Execute() { c.editor.Insert(c.text) }

func (c *InsertCommand) Undo() { // 撤销插入:删除最后插入的内容 c.editor.Delete(len(c.text)) }

删除命令

type DeleteCommand struct {
    editor     *TextEditor
    deleted    string  // 保存被删除的内容,用于撤销
}

func NewDeleteCommand(editor TextEditor, length int) DeleteCommand { end := len(editor.content) start := end - length if start < 0 { start = 0 } deleted := editor.content[start:end] return &DeleteCommand{ editor: editor, deleted: deleted, } }

func (c *DeleteCommand) Execute() { c.editor.Delete(len(c.deleted)) }

func (c *DeleteCommand) Undo() { // 撤销删除:重新插入之前删掉的内容 c.editor.Insert(c.deleted) }

调用者:支持撤销的命令管理器

命令调用者负责执行命令并维护历史记录:

type CommandHistory struct {
    history []Command
    index   int // 当前位置,支持未来可能的重做
}

func NewCommandHistory() *CommandHistory { return &CommandHistory{ history: make([]Command, 0), index: -1, } }

func (ch *CommandHistory) Execute(command Command) { // 执行新命令时,丢弃“未来”的命令(重做部分) ch.history = ch.history[:ch.index+1] ch.index++ ch.history = append(ch.history, command) command.Execute() }

func (ch *CommandHistory) Undo() { if ch.index >= 0 { command := ch.history[ch.index] command.Undo() ch.index-- } }

客户端使用示例

现在将所有组件组合起来:

func main() {
    editor := &TextEditor{}
    history := NewCommandHistory()
// 插入 "Hello"
cmd1 := NewInsertCommand(editor, "Hello")
history.Execute(cmd1)
fmt.Println("After insert 'Hello':", editor.GetContent()) // Hello

// 插入 " World"
cmd2 := NewInsertCommand(editor, " World")
history.Execute(cmd2)
fmt.Println("After insert ' World':", editor.GetContent()) // Hello World

// 撤销一次
history.Undo()
fmt.Println("After undo:", editor.GetContent()) // Hello

// 撤销第二次
history.Undo()
fmt.Println("After second undo:", editor.GetContent()) // ""

// 删除部分内容再撤销
cmd3 := NewDeleteCommand(editor, 5)
history.Execute(cmd3)
fmt.Println("After delete 5 chars:", editor.GetContent()) // ""

history.Undo()
fmt.Println("After undo delete:", editor.GetContent()) // Hello

}

总结

通过命令模式,Golang可以优雅地实现撤销功能。关键点在于:

  • 每个操作封装为命令对象,包含正向执行和反向撤销逻辑。
  • 命令对象保存足够的上下文信息(如删除内容),以便正确撤销。
  • 使用历史栈管理命令顺序,支持多级撤销。

这种模式让新增操作变得容易,符合开闭原则,也提升了代码的可维护性。在实际项目中,还可扩展支持重做(Redo)、事务型命令、命令组合等功能。

基本上就这些。

相关专题

更多
golang如何定义变量
golang如何定义变量

golang定义变量的方法:1、声明变量并赋予初始值“var age int =值”;2、声明变量但不赋初始值“var age int”;3、使用短变量声明“age :=值”等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

178

2024.02.23

golang有哪些数据转换方法
golang有哪些数据转换方法

golang数据转换方法:1、类型转换操作符;2、类型断言;3、字符串和数字之间的转换;4、JSON序列化和反序列化;5、使用标准库进行数据转换;6、使用第三方库进行数据转换;7、自定义数据转换函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

226

2024.02.23

golang常用库有哪些
golang常用库有哪些

golang常用库有:1、标准库;2、字符串处理库;3、网络库;4、加密库;5、压缩库;6、xml和json解析库;7、日期和时间库;8、数据库操作库;9、文件操作库;10、图像处理库。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

337

2024.02.23

golang和python的区别是什么
golang和python的区别是什么

golang和python的区别是:1、golang是一种编译型语言,而python是一种解释型语言;2、golang天生支持并发编程,而python对并发与并行的支持相对较弱等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

208

2024.03.05

golang是免费的吗
golang是免费的吗

golang是免费的。golang是google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的开源编程语言,采用bsd开源协议。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

391

2024.05.21

golang结构体相关大全
golang结构体相关大全

本专题整合了golang结构体相关大全,想了解更多内容,请阅读专题下面的文章。

196

2025.06.09

golang相关判断方法
golang相关判断方法

本专题整合了golang相关判断方法,想了解更详细的相关内容,请阅读下面的文章。

191

2025.06.10

golang数组使用方法
golang数组使用方法

本专题整合了golang数组用法,想了解更多的相关内容,请阅读专题下面的文章。

192

2025.06.17

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

2

2026.01.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 3.8万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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