【设计模式】第16节:行为型模式之“命令模式”

一、简介

命令模式:将请求(命令)封装为一个对象,这样可以使用不同的请求参数化其他对象(将不同请求依赖注入到其他对象),并且能够支持请求(命令)的排队执行、记录日志、撤销等(附加控制)功能。

二、优点

  • 动作封装
  • 解耦发送者跟接受者
  • 可扩展性
  • 简化和集中错误处理
  • 支持撤销和重做功能
  • 易于实现组合命令

三、UML类图

四、案例

家里有很多房间,有卧室和厨房等,用一套灯光管理系统同一管理所有灯的开关。

go 复制代码
package main

import "fmt"

type Light interface {
	On()
	Off()
}

type KitchenLight struct {
}

func NewKitchenLight() *KitchenLight {
	return &KitchenLight{}
}

func (*KitchenLight) On() {
	fmt.Println("Kitchen light is on")
}

func (*KitchenLight) Off() {
	fmt.Println("Kitchen light is off")
}

type LivingRoomLight struct {
}

func NewLivingRoomLight() *LivingRoomLight {
	return &LivingRoomLight{}
}

func (*LivingRoomLight) On() {
	fmt.Println("Living room light is on")
}

func (*LivingRoomLight) Off() {
	fmt.Println("Living room light is off")
}

type Command interface {
	Execute()
	Undo()
}

type LightOnCommand struct {
	Lights []Light
}

func NewLightOnCommand(lights []Light) LightOnCommand {
	return LightOnCommand{Lights: lights}
}

func (loc *LightOnCommand) Execute() {
	for _, light := range loc.Lights {
		light.On()
	}
}

func (loc *LightOnCommand) Undo() {
	for _, light := range loc.Lights {
		light.Off()
	}
}

type LightOffCommand struct {
	Lights []Light
}

func NewLightOffCommand(lights []Light) LightOffCommand {
	return LightOffCommand{Lights: lights}
}

func (loc *LightOffCommand) Execute() {
	for _, light := range loc.Lights {
		light.Off()
	}
}

func (loc *LightOffCommand) Undo() {
	for _, light := range loc.Lights {
		light.On()
	}
}

func main() {
	kitchenLight := NewKitchenLight()
	livingRoomLight := NewLivingRoomLight()

	lightOnCommand := NewLightOnCommand([]Light{kitchenLight, livingRoomLight})
	lightOnCommand.Execute()
	lightOnCommand.Undo()

	lightOffCommand := NewLightOffCommand([]Light{kitchenLight, livingRoomLight})
	lightOffCommand.Execute()
	lightOffCommand.Undo()
}

五、对比

命令模式与策略模式的区别:在策略模式中,不同的策略具有相同的目的、不同的实现、互相之间可以替换。比如,BubbleSort、SelectionSort都是为了实现排序的,只不过一个是用冒泡排序算法来实现的,另一个是用选择排序算法来实现的。而在命令模式中,不同的命令具有不同的目的,对应不同的处理逻辑,并且互相之间不可替换。

相关推荐
不一样的少年_2 小时前
Vue3 后台分页写腻了?我用 1 个 Hook 删掉 90% 重复代码(附源码)
前端·vue.js·设计模式
A阳俊yi2 小时前
设计模式——七大常见设计原则
设计模式
青草地溪水旁4 小时前
设计模式(C++)详解——建造者模式(1)
c++·设计模式·建造者模式
念何架构之路9 小时前
Go语言设计模式(七)组合模式
设计模式·组合模式
易元17 小时前
模式组合应用-外观模式
后端·设计模式
大飞pkz21 小时前
【设计模式】题目小练1
开发语言·设计模式·c#·题目小练
烛阴1 天前
【TS 设计模式完全指南】TypeScript 装饰器模式的优雅之道
javascript·设计模式·typescript
E___V___E1 天前
设计模式--装饰器模式
python·设计模式·装饰器模式
TechNomad1 天前
设计模式:访问者模式(Visitor Pattern)
设计模式·访问者模式
hweiyu002 天前
C++设计模式,高级开发,算法原理实战,系统设计与实战(视频教程)
c++·算法·设计模式