登录
首页 >  Golang >  Go教程

Golang命令模式详解:请求与操作封装实现

时间:2025-12-29 11:18:36 355浏览 收藏

你在学习Golang相关的知识吗?本文《Golang命令模式实现:封装请求与操作执行》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

命令模式的核心是将请求封装为对象以解耦发送者与执行者;Go中通过接口和结构体组合实现,支持Execute/Undo方法、调用者调度及闭包简化命令。

如何使用Golang实现命令模式_封装请求和执行操作

命令模式的核心是把请求封装成对象,使发出请求的对象与执行请求的对象解耦。在 Go 中没有接口继承和类的概念,但通过接口(interface{})和结构体组合,可以简洁、自然地实现命令模式。

定义命令接口和具体命令

先定义一个通用的 Command 接口,要求所有命令都实现 Execute() 方法:

type Command interface {
    Execute()
}

然后为不同操作创建具体命令结构体。例如,实现一个开关灯的命令:

type Light struct {
    isOn bool
}
<p>func (l <em>Light) TurnOn()  { l.isOn = true; fmt.Println("灯已打开") }
func (l </em>Light) TurnOff() { l.isOn = false; fmt.Println("灯已关闭") }</p><p>type LightOnCommand struct {
light *Light
}</p><p>func (c *LightOnCommand) Execute() {
c.light.TurnOn()
}</p><p>type LightOffCommand struct {
light *Light
}</p><p>func (c *LightOffCommand) Execute() {
c.light.TurnOff()
}</p>

引入调用者(Invoker)统一调度

调用者不关心命令具体做什么,只负责持有并执行命令。它通常提供设置命令和触发执行的方法:

type RemoteControl struct {
    command Command
}
<p>func (r *RemoteControl) SetCommand(cmd Command) {
r.command = cmd
}</p><p>func (r *RemoteControl) PressButton() {
if r.command != nil {
r.command.Execute()
}
}</p>

使用方式很直观:

light := &Light{}
onCmd := &LightOnCommand{light: light}
offCmd := &LightOffCommand{light: light}
<p>remote := &RemoteControl{}</p><p>remote.SetCommand(onCmd)
remote.PressButton() // 输出:灯已打开</p><p>remote.SetCommand(offCmd)
remote.PressButton() // 输出:灯已关闭</p>

支持撤销操作(可选增强)

若需支持撤销,可在命令接口中增加 Undo() 方法,并让具体命令保存执行前的状态:

type Command interface {
    Execute()
    Undo()
}
<p>func (c *LightOnCommand) Undo() {
c.light.TurnOff()
}</p><p>func (c *LightOffCommand) Undo() {
c.light.TurnOn()
}</p>

调用者也可扩展支持撤销:

type RemoteControl struct {
    command Command
    history []Command // 简单记录最近执行的命令,用于撤销
}
<p>func (r *RemoteControl) PressButton() {
if r.command != nil {
r.command.Execute()
r.history = append(r.history, r.command)
}
}</p><p>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]
}
}</p>

利用闭包简化简单命令(轻量替代方案)

对于逻辑简单的操作,不必定义完整结构体,可用闭包快速构造命令:

type FuncCommand func()
<p>func (f FuncCommand) Execute() { f() }
func (f FuncCommand) Undo()     { /<em> 可按需实现 </em>/ }</p><p>// 使用示例
cmd := FuncCommand(func() {
fmt.Println("执行自定义操作")
})
var invoker RemoteControl
invoker.SetCommand(cmd)
invoker.PressButton()</p>

这种方式灵活、无侵入,适合脚本化或配置驱动的场景。

好了,本文到此结束,带大家了解了《Golang命令模式详解:请求与操作封装实现》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>