【设计模式】第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都是为了实现排序的,只不过一个是用冒泡排序算法来实现的,另一个是用选择排序算法来实现的。而在命令模式中,不同的命令具有不同的目的,对应不同的处理逻辑,并且互相之间不可替换。

相关推荐
狂人开飞机13 小时前
01. 工厂模式(Factory Pattern)
设计模式·c#
阿狸猿14 小时前
论软件设计模式及其应用
设计模式
workflower14 小时前
具身智能-三层结构
人工智能·设计模式·动态规划·软件工程·scrum
我爱cope15 小时前
【Agent智能体10 | 反思设计模式-AI数据分析的可视化实战】
人工智能·设计模式·数据分析
老码观察15 小时前
设计模式实战解读(七):适配器模式——让不兼容的接口无缝协作
java·设计模式·适配器模式
人月神话-Lee2 天前
【图像处理】框架设计——协议、值类型与工程化思维
图像处理·人工智能·ios·设计模式·架构·ai编程·swift
AI大法师2 天前
Xbox回归经典绿
大数据·设计模式·xbox
老码观察2 天前
设计模式实战解读(六):装饰器模式——功能增强,不动原代码
java·设计模式·装饰器模式
Doris_20232 天前
代码格式化 使用oxfmt
设计模式·架构·前端框架