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

命令模式在 Golang 中常用于将请求封装为对象,从而支持请求的排队、记录日志、撤销等操作。结合事件队列后,可以实现异步处理、解耦调用者与执行者,适用于任务调度、GUI 操作、后台作业等场景。
命令接口与具体命令实现
定义统一的命令接口,每个命令实现自己的 Execute 方法。这样可以在队列中统一处理不同类型的命令。
type Command interface {
Execute()
}
以文件操作为例,定义一个“保存文件”命令:
type SaveFileCommand struct {
Filename string
Content string
}
func (c SaveFileCommand) Execute() {
fmt.Printf("正在保存文件: %s, 内容长度: %d\n", c.Filename, len(c.Content))
// 模拟写入文件
time.Sleep(100 time.Millisecond)
}
再定义一个“删除文件”命令:
立即学习“go语言免费学习笔记(深入)”;
type DeleteFileCommand struct {
Filename string
}
func (c *DeleteFileCommand) Execute() {
fmt.Printf("正在删除文件: %s\n", c.Filename)
}
事件队列与异步处理器
使用带缓冲的 channel 作为事件队列,启动一个或多个 goroutine 异步消费命令。
type CommandQueue struct {
commands chan Command
wg sync.WaitGroup
}
func NewCommandQueue(bufferSize int) *CommandQueue {
return &CommandQueue{
commands: make(chan Command, bufferSize),
}
}
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()
}
}()
}
}
func (q *CommandQueue) AddCommand(cmd Command) {
q.commands <- cmd
}
func (q *CommandQueue) Close() {
close(q.commands)
q.wg.Wait()
}
实际使用示例
将多个命令提交到队列,由后台 worker 异步执行。
func main() {
queue := NewCommandQueue(10)
queue.Start(2) // 启动两个工作协程
// 提交多个命令
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
}
func (c *SaveFileCommand) Execute() {
// 读取旧内容(此处简化)
c.OldContent = "旧内容备份"
c.WasExisting = true
fmt.Printf("已保存: %s\n", c.Filename)
}
func (c *SaveFileCommand) Undo() {
if c.WasExisting {
fmt.Printf("已还原文件 %s 的内容\n", c.Filename)
} else {
fmt.Printf("已删除新建文件 %s\n", c.Filename)
}
}
可在队列中记录已执行命令,供后续批量撤销使用。
基本上就这些。通过命令模式 + channel 队列,Golang 能轻松实现解耦、异步、可扩展的任务系统。










