C# 23种设计模式(5)命令模式(Command Pattern)

一、命令模式介绍

命令模式(Command Pattern)是一种行为设计模式 ,它将一个请求封装为一个对象 ,从而允许用户用不同的请求、队列请求、记录请求日志,以及支持可撤销的操作等。命令模式的主要目的 是++将请求的执行者请求的接收者解耦,使得请求发送者和接收者之间不必直接交互++。

命令模式参与者:

命令的接收者(接收者Receiver):

  • 它单纯的只具体实现了功能。(实例中对应的则是张三和李四)

命令的请求者(调用者Invoker):

  • 就是起到遥控的作用,首先在类中声明抽象命令类的引用,并通过参数注入等方式进行实例化。
  • 然后通过调用命令对象来告诉执行者执行功能。起到一个传声简的作用。(实例中对应的是宦官)

抽象命令类Command:

  • 主要作用有两点,其一就是为了规范具体命令类,声明其执行方法。
  • 其二就是为了方便调用者Invoker调用,使其不需要知道具体命令类,只需要执行抽象命令类即可。(实例中对应的是泛指圣旨)

具体命令类ConcreteCommand:

  • 继承自抽象类,在类中首先声明了执行者的引用,通过参数注入等方式进行创建执行者对象,将命令类和执行者进行绑定。
  • 其次具体实现了抽象类中声明的执行方法,调用执行者对象中方法进行执行。(实例中对应的是张三和李四具体的两道圣旨)

客户端角色client:

  • 主要是负责将执行者和命令类进行绑定,其次将命令类和调用者进行绑定。
  • 使其调用者可以通过命令类给执行者传递消息进行执行。(实例中对应的是皇帝角色)

二、命令模式代码实现

cs 复制代码
using System;
using System.Collections.Generic;

// 命令接口
public interface ICommand
{
    void Execute();
    void Undo();
}

// 具体命令类 - 打开灯光
public class LightOnCommand : ICommand
{
    private LightReceiver lightReceiver;

    public LightOnCommand(LightReceiver lightReceiver)
    {
        this.lightReceiver = lightReceiver;
    }

    public void Execute()
    {
        lightReceiver.On();
        Console.WriteLine("Light is on");
    }

    public void Undo()
    {
        lightReceiver.Off();
        Console.WriteLine("Light is off");
    }
}

// 具体命令类 - 关闭灯光
public class LightOffCommand : ICommand
{
    private LightReceiver lightReceiver;

    public LightOffCommand(LightReceiver lightReceiver)
    {
        this.lightReceiver = lightReceiver;
    }

    public void Execute()
    {
        lightReceiver.Off();
        Console.WriteLine("Light is off");
    }

    public void Undo()
    {
        lightReceiver.On();
        Console.WriteLine("Light is on");
    }
}

// 请求者类 - 可以执行和撤销命令
public class Invoker
{
    private List<ICommand> history = new List<ICommand>();

    public void ExecuteCommand(ICommand command)
    {
        command.Execute();
        history.Add(command);
    }

    public void UndoCommand()
    {
        if (history.Count > 0)
        {
            ICommand command = history[history.Count - 1];
            command.Undo();
            history.RemoveAt(history.Count - 1);
        }
        else
        {
            Console.WriteLine("No command to undo");
        }
    }
}

// 接收者类 - 知道如何执行具体的操作
public class LightReceiver
{
    public void On()
    {
        Console.WriteLine("Receiver's On method called");
    }

    public void Off()
    {
        Console.WriteLine("Receiver's Off method called");
    }
}

// 客户端代码
public class Client
{
    public static void Main(string[] args)
    {
        LightReceiver light = new LightReceiver();

        ICommand lightOnCommand = new LightOnCommand(light);
        ICommand lightOffCommand = new LightOffCommand(light);

        Invoker invoker = new Invoker();

        invoker.ExecuteCommand(lightOnCommand);
        invoker.ExecuteCommand(lightOffCommand);
        invoker.UndoCommand();
    }
}
  1. ICommand 接口 :声明了 ExecuteUndo 方法。
  2. LightOnCommand 和 LightOffCommand 类 :实现了 ICommand 接口,并持有 LightReceiver 对象。Execute 方法调用接收者的 OnOff 方法,Undo 方法则相反。
  3. Invoker 类:维护了一个命令的历史列表,可以执行和撤销命令。
  4. LightReceiver 类:知道如何打开和关闭灯光。
  5. Client 类:创建具体的命令对象,并设置接收者,然后将命令对象传递给请求者。

通过这种方式,命令模式实现了请求发送者和接收者之间的解耦,并且支持命令的记录和撤销

相关推荐
laimaxgg几秒前
Qt常用控件之数字显示控件QLCDNumber
开发语言·c++·qt·qt5·qt6.3
蓝天扶光4 分钟前
c++贪心系列
开发语言·c++
奔跑吧邓邓子32 分钟前
【Python爬虫(44)】分布式爬虫:筑牢安全防线,守护数据之旅
开发语言·分布式·爬虫·python·安全
C#Thread1 小时前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
万兴丶1 小时前
Unity 适用于单机游戏的红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含源码)
数据结构·unity·设计模式·c#
牵牛老人1 小时前
Qt开发中出现中文乱码问题深度解析与解决方案
开发语言·qt
大脑经常闹风暴@小猿1 小时前
1.1 go环境搭建及基本使用
开发语言·后端·golang
菜鸟一枚在这1 小时前
深入剖析抽象工厂模式:设计模式中的架构利器
设计模式·架构·抽象工厂模式
码熔burning1 小时前
(三)趣学设计模式 之 抽象工厂模式!
设计模式·抽象工厂模式
奔跑吧邓邓子2 小时前
【Python爬虫(45)】Python爬虫新境界:分布式与大数据框架的融合之旅
开发语言·分布式·爬虫·python·大数据框架