命令模式通过将请求封装为对象,支持异步处理与撤销操作;结合事件队列可实现解耦与任务调度。

命令模式在 Golang 中常用于将请求封装为对象,从而支持请求的排队、记录日志、撤销等操作。结合事件队列后,可以实现异步处理、解耦调用者与执行者,适用于任务调度、GUI 操作、后台作业等场景。
定义统一的命令接口,每个命令实现自己的 Execute 方法。这样可以在队列中统一处理不同类型的命令。
type Command interface {
Execute()
}
以文件操作为例,定义一个“保存文件”命令:
type SaveFileCommand struct {
Filename string
Content string
}
<p>func (c <em>SaveFileCommand) Execute() {
fmt.Printf("正在保存文件: %s, 内容长度: %d\n", c.Filename, len(c.Content))
// 模拟写入文件
time.Sleep(100 </em> time.Millisecond)
}</p>再定义一个“删除文件”命令:
立即学习“go语言免费学习笔记(深入)”;
type DeleteFileCommand struct {
Filename string
}
<p>func (c *DeleteFileCommand) Execute() {
fmt.Printf("正在删除文件: %s\n", c.Filename)
}</p>使用带缓冲的 channel 作为事件队列,启动一个或多个 goroutine 异步消费命令。
type CommandQueue struct {
commands chan Command
wg sync.WaitGroup
}
<p>func NewCommandQueue(bufferSize int) *CommandQueue {
return &CommandQueue{
commands: make(chan Command, bufferSize),
}
}</p><p>func (q *CommandQueue) Start(workers int) {
for i := 0; i < workers; i++ {
q.wg.Add(1)
go func() {
defer q.wg.Done()
for cmd := range q.commands {
cmd.Execute()
}
}()
}
}</p><p>func (q *CommandQueue) AddCommand(cmd Command) {
q.commands <- cmd
}</p><p>func (q *CommandQueue) Close() {
close(q.commands)
q.wg.Wait()
}</p>将多个命令提交到队列,由后台 worker 异步执行。
func main() {
queue := NewCommandQueue(10)
queue.Start(2) // 启动两个工作协程
<pre class='brush:php;toolbar:false;'>// 提交多个命令
queue.AddCommand(&SaveFileCommand{
Filename: "report.txt",
Content: "年度报告内容",
})
queue.AddCommand(&DeleteFileCommand{
Filename: "temp.log",
})
queue.AddCommand(&SaveFileCommand{
Filename: "config.json",
Content: `{"mode": "debug"}`,
})
// 关闭队列并等待所有任务完成
queue.Close()
fmt.Println("所有命令已处理完毕")}
输出类似:
正在保存文件: report.txt, 内容长度: 14 正在删除文件: temp.log 正在保存文件: config.json, 内容长度: 16 所有命令已处理完毕
命令模式天然支持撤销操作。可在命令中增加 Undo 方法:
type UndoableCommand interface {
Command
Undo()
}
例如,保存文件命令可记录原内容用于恢复:
type SaveFileCommand struct {
Filename string
NewContent string
OldContent string
WasExisting bool
}
<p>func (c *SaveFileCommand) Execute() {
// 读取旧内容(此处简化)
c.OldContent = "旧内容备份"
c.WasExisting = true
fmt.Printf("已保存: %s\n", c.Filename)
}</p><p>func (c *SaveFileCommand) Undo() {
if c.WasExisting {
fmt.Printf("已还原文件 %s 的内容\n", c.Filename)
} else {
fmt.Printf("已删除新建文件 %s\n", c.Filename)
}
}</p>可在队列中记录已执行命令,供后续批量撤销使用。
基本上就这些。通过命令模式 + channel 队列,Golang 能轻松实现解耦、异步、可扩展的任务系统。
以上就是如何使用Golang实现命令模式与事件队列结合_Golang 命令模式高级示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号