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 类:创建具体的命令对象,并设置接收者,然后将命令对象传递给请求者。

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

相关推荐
Takoony4 分钟前
理解 Python ProcessPoolExecutor 的序列化问题:为什么线程锁(threading.Lock)会导致异常?
linux·开发语言·python
道友老李5 分钟前
【Python基础】入门(运算、输出、数据类型)
开发语言·python
missu2178 分钟前
C++中如何实现单例模式?
开发语言·c++·单例模式
霍格沃兹测试开发学社测试人社区20 分钟前
不同协议下的接口测试方案设计
开发语言·软件测试·测试开发·自动化
汝即来归22 分钟前
如何实现序列化和反序列化?如何处理对象的生命周期管理?
开发语言·python
豆本-豆豆奶29 分钟前
Python 图像处理领域的十一个基础操作
开发语言·图像处理·python
lsx20240638 分钟前
XSLT 编辑 XML
开发语言
heirui_Oooo41 分钟前
第五章:IO流 (java.io包中)
java·开发语言
未来可期LJ1 小时前
【C++复习第5小节】类和对象
开发语言·c++
~狂想家~1 小时前
使用C语言库函数格式化输入时格式类型与数据类型不匹配导致程序异常
c语言·开发语言