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

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

相关推荐
Joker—H3 分钟前
【Java】Reflection反射(代理模式)
java·开发语言·经验分享·代理模式·idea
阿里巴巴淘系技术团队官网博客31 分钟前
面向互联网2C业务的分布式类Manus Java框架
java·开发语言·分布式
笨蛋不要掉眼泪1 小时前
Java测试题(上)
java·开发语言
东方佑2 小时前
高效序列建模新突破:SamOut模型解读与21.79%损失改进
开发语言·python
峥无2 小时前
C语言分支与循环深度解析
c语言·开发语言
不过普通话一乙不改名2 小时前
第一章:Go语言基础入门之函数
开发语言·后端·golang
★YUI★2 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
屁股割了还要学3 小时前
【C语言进阶】柔性数组
c语言·开发语言·数据结构·c++·学习·算法·柔性数组
☞下凡☜3 小时前
C语言(20250722)
linux·c语言·开发语言
whhhhhhhhhw3 小时前
Go语言-fmt包中Print、Println与Printf的区别
开发语言·后端·golang