命令模式将请求封装为对象,实现发送者与接收者的解耦。Go通过接口和组合实现该模式:定义Command接口及具体命令如LightOnCommand,由Receiver(如Light)执行实际逻辑,Invoker(如RemoteControl)触发命令,Client组装并传递命令。支持扩展Undo操作,适用于可撤销、队列化请求的场景。

在Go语言中,命令模式是一种行为设计模式,它将请求封装为对象,从而使你可以用不同的请求、队列或日志来参数化其他对象。命令模式也支持可撤销的操作。对于需要解耦请求发送者和接收者的场景非常有用。
命令模式通常包含以下几个部分:
下面是一个简单的例子,模拟一个远程控制器控制灯的开关操作。
1. 定义接收者(Receiver)接收者是实际执行操作的对象:
立即学习“go语言免费学习笔记(深入)”;
type Light struct{}
func (l *Light) TurnOn() {
fmt.Println("灯已打开")
}
func (l *Light) TurnOff() {
fmt.Println("灯已关闭")
}
命令接口统一执行方法,具体命令封装不同操作:
type Command interface {
Execute()
}
type LightOnCommand struct {
light *Light
}
func (c *LightOnCommand) Execute() {
c.light.TurnOn()
}
type LightOffCommand struct {
light *Light
}
func (c *LightOffCommand) Execute() {
c.light.TurnOff()
}
调用者不关心命令的具体实现,只负责触发执行:
type RemoteControl struct {
command Command
}
func (r *RemoteControl) PressButton() {
if r.command != nil {
r.command.Execute()
}
}
组装命令并交由调用者执行:
func main() {
// 接收者
light := &Light{}
// 具体命令
onCommand := &LightOnCommand{light: light}
offCommand := &LightOffCommand{light: light}
// 调用者
remote := &RemoteControl{}
// 执行开灯
remote.command = onCommand
remote.PressButton()
// 执行关灯
remote.command = offCommand
remote.PressButton()
}
输出结果:
灯已打开 灯已关闭
命令模式天然适合实现撤销功能。只需在命令接口中添加 Undo 方法:
type Command interface {
Execute()
Undo()
}
// 修改 LightOnCommand
func (c *LightOnCommand) Undo() {
c.light.TurnOff()
}
// 修改 LightOffCommand
func (c *LightOffCommand) Undo() {
c.light.TurnOn()
}
// RemoteControl 可记录上一次命令
type RemoteControl struct {
command Command
history []Command
}
func (r *RemoteControl) PressButton() {
if r.command != nil {
r.command.Execute()
r.history = append(r.history, r.command)
}
}
func (r *RemoteControl) UndoLast() {
if len(r.history) > 0 {
last := r.history[len(r.history)-1]
last.Undo()
r.history = r.history[:len(r.history)-1]
}
}
这样就可以实现操作的回退,适用于需要事务性控制的场景。
基本上就这些。Go虽然没有类继承,但通过接口和组合可以很自然地实现命令模式,让操作变成可传递、可存储、可撤销的一等公民。这种封装方式在构建工作流、菜单系统、远程调用等场景中特别实用。不复杂但容易忽略的是,保持命令轻量,真正逻辑放在接收者中。
以上就是Golang如何使用命令模式封装操作的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号