命令模式将操作封装为对象,解耦请求发起者与接收者。在Go中,通过接口定义Command,结构体实现具体命令,绑定Receiver执行动作,Invoker调用Execute,支持Undo与历史记录,适用于任务队列、GUI按钮、宏命令等场景。

命令模式是一种行为设计模式,它将请求封装为对象,从而使你可以用不同的请求、队列或日志来参数化其他对象。在 Golang 中,虽然没有类和继承这样的传统面向对象特性,但通过接口和结构体的组合,依然可以优雅地实现命令模式。
命令模式的核心思想
命令模式的关键在于把“操作”当成一个对象。这个对象知道要执行什么动作、由谁执行,并能触发执行。它解耦了发起请求的对象和接收请求的对象,提升了系统的灵活性和可扩展性。
典型结构包括:
- Command 接口:定义执行方法(如 Execute)
- 具体命令:实现接口,持有接收者并调用其方法
- Receiver:真正执行操作的对象
- Invoker:调用命令对象执行请求
- Client:创建命令并绑定接收者
用 Go 实现基本命令模式
假设我们要实现一个遥控器控制灯的开关功能。
立即学习“go语言免费学习笔记(深入)”;
package main import "fmt" // Receiver:实际执行操作的对象 type Light struct {} func (l *Light) On() { fmt.Println("灯打开了") } func (l *Light) Off() { fmt.Println("灯关闭了") } // Command 接口 type Command interface { Execute() } // 具体命令:开灯 type LightOnCommand struct { light *Light } func (c *LightOnCommand) Execute() { c.light.On() } // 具体命令:关灯 type LightOffCommand struct { light *Light } func (c *LightOffCommand) Execute() { c.light.Off() } // Invoker:触发命令 type RemoteControl struct { command Command } func (r *RemoteControl) PressButton() { if r.command != nil { r.command.Execute() } } func main() { light := &Light{} onCmd := &LightOnCommand{light: light} offCmd := &LightOffCommand{light: light} remote := &RemoteControl{} remote.command = onCmd remote.PressButton() // 输出:灯打开了 remote.command = offCmd remote.PressButton() // 输出:灯关闭了 }
支持撤销操作的命令模式
命令模式的优势之一是容易支持撤销(Undo)。每个命令除了 Execute,还可以实现 Undo 方法。
type Command interface { Execute() Undo() } func (c *LightOnCommand) Undo() { c.light.Off() } func (c *LightOffCommand) Undo() { c.light.On() } // 可记录历史用于撤销 type RemoteControlWithUndo struct { command Command history []Command } func (r *RemoteControlWithUndo) PressButton() { if r.command != nil { r.command.Execute() r.history = append(r.history, r.command) } } func (r *RemoteControlWithUndo) Undo() { if len(r.history) > 0 { last := r.history[len(r.history)-1] last.Undo() r.history = r.history[:len(r.history)-1] } }
这样就可以实现按一次撤销上一个操作,适合做事务性控制或 UI 操作回退。
实际应用场景建议
命令模式适用于需要将操作抽象化的场景:
- 任务队列系统:把任务封装成命令,放入队列异步执行
- 远程调用或 RPC:命令序列化后传输执行
- GUI 按钮操作:不同按钮绑定不同命令
- 宏命令(Macro Command):一组命令组合执行
例如,在后台管理系统中,可以把“创建用户”、“发送邮件”、“记录日志”等封装为命令链,统一调度。
基本上就这些。Go 虽然不是面向对象语言,但通过接口和组合,完全可以实现干净的命令模式结构,让代码更灵活、易测试、可扩展。










