设计模式——Go语言(Golang)版:22_命令模式

本文介绍了命令模式的概念,它将请求封装为对象,便于参数化客户端、队列请求、记录日志及支持撤销操作。示例展示了如何使用命令模式来控制电脑主板的启动和重启,通过创建不同命令对象并由按钮触发执行。命令模式中包含Command接口、具体命令实现、调用者(Invoker)和接收者(Receiver)等角色。
摘要由CSDN通过智能技术生成

1、介绍

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作。

命令模式(Command)是指,把请求封装成一个命令,然后执行该命令。

命令模式的结构

        顾名思义,命令模式就是对命令的封装,首先来看一下命令模式类图中的基本结构:

Command类:是一个抽象类,类中对需要执行的命令进行声明,一般来说要对外公布一个execute方法用来执行命令。
ConcreteCommand类:Command类的实现类,对抽象类中声明的方法进行实现。
Client类:最终的客户端调用类。

Invoker类:调用者,负责调用命令。
Receiver类:接收者,负责接收命令并且执行命令。

2、示例

示例代码:

package main

import "fmt"

type Command interface {
	Execute()
}
type MotherBoard struct{}

func (*MotherBoard) Start() {
	fmt.Println("系统启动中...")
}
func (*MotherBoard) Reboot() {
	fmt.Println("系统重启中...")
}

//====启动命令=====
type StartCommand struct {
	mb *MotherBoard
}

func (c *StartCommand) Execute() {
	c.mb.Start()
}
func NewStartCommand(mb *MotherBoard) *StartCommand {
	return &StartCommand{
		mb: mb,
	}
}

//====重启命令=====
type RebootCommand struct {
	mb *MotherBoard
}

func (c *RebootCommand) Execute() {
	c.mb.Reboot()
}
func NewRebootCommand(mb *MotherBoard) *RebootCommand {
	return &RebootCommand{
		mb: mb,
	}
}
//====按钮=====
type Box struct {
	button1 Command
	button2 Command
}

func (b *Box) PressButton1() {
	b.button1.Execute()
}
func (b *Box) PressButton2() {
	b.button2.Execute()
}
func NewBox(btn1, btn2 Command) *Box {
	return &Box{
		button1: btn1,
		button2: btn2,
	}
}
func main() {
	mb:=&MotherBoard{}
	btn1:=NewStartCommand(mb)
	btn2:=NewRebootCommand(mb)
	box := NewBox(btn1, btn2)
	box.PressButton1()
	box.PressButton2()
}

UML图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值