c#实现命令模式

下面是一个使用C#实现命令模式的示例代码:

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

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

// 具体命令:打开文件
public class OpenFileCommand : ICommand
{
    private FileManager fileManager;

    public OpenFileCommand(FileManager fileManager)
    {
        this.fileManager = fileManager;
    }

    public void Execute()
    {
        fileManager.Open();
    }

    public void Undo()
    {
        fileManager.Close();
    }
}

// 具体命令:保存文件
public class SaveFileCommand : ICommand
{
    private FileManager fileManager;

    public SaveFileCommand(FileManager fileManager)
    {
        this.fileManager = fileManager;
    }

    public void Execute()
    {
        fileManager.Save();
    }

    public void Undo()
    {
        fileManager.UndoSave();
    }
}

// 接收者:文件管理器
public class FileManager
{
    public void Open()
    {
        Console.WriteLine("打开文件");
    }

    public void Close()
    {
        Console.WriteLine("关闭文件");
    }

    public void Save()
    {
        Console.WriteLine("保存文件");
    }

    public void UndoSave()
    {
        Console.WriteLine("撤销保存文件");
    }
}

// 调用者:命令执行器
public class CommandExecutor
{
    private List<ICommand> commands = new List<ICommand>();

    public void AddCommand(ICommand command)
    {
        commands.Add(command);
    }

    public void ExecuteCommands()
    {
        foreach (var command in commands)
        {
            command.Execute();
        }
        commands.Clear();
    }

    public void UndoCommands()
    {
        for (int i = commands.Count - 1; i >= 0; i--)
        {
            commands[i].Undo();
        }
        commands.Clear();
    }
}

// 示例代码
class Program
{
    static void Main(string[] args)
    {
        FileManager fileManager = new FileManager();

        OpenFileCommand openCommand = new OpenFileCommand(fileManager);
        SaveFileCommand saveCommand = new SaveFileCommand(fileManager);

        CommandExecutor executor = new CommandExecutor();
        executor.AddCommand(openCommand);
        executor.AddCommand(saveCommand);

        executor.ExecuteCommands();
        executor.UndoCommands();

        Console.ReadKey();
    }
}

在上述示例中,我们首先定义了一个命令接口ICommand,它包含了Execute和Undo方法。然后我们创建了两个具体的命令类OpenFileCommand和SaveFileCommand,分别表示打开文件和保存文件的命令。

接下来,我们创建了一个接收者FileManager,它是实际执行命令的对象。FileManager中包含了一些具体的操作方法,比如打开文件、关闭文件、保存文件等。

最后,我们创建了一个调用者CommandExecutor,它可以添加和执行命令。调用者将命令添加到命令列表中,并在需要时一起执行或撤销。

在示例代码中,我们创建了一个CommandExecutor对象,添加了打开文件和保存文件的命令。然后我们依次执行这些命令,并在最后撤销执行的命令。运行代码后,你将看到打开文件、关闭文件、保存文件和撤销保存文件的输出。

通过使用命令模式,我们可以将操作封装成命令对象,并在需要时进行执行或撤销。这种方式可以实现请求和执行解耦,同时也方便扩展和管理多个命令。

相关推荐
hixiong1235 小时前
TensorRT转换工具分享
人工智能·计算机视觉·ai·c#
饼干哥哥5 小时前
用Claude Code跑通跨境电商的5大场景,顶10个人的团队
人工智能·设计模式·正则表达式
折哥的程序人生 · 物流技术专研6 小时前
第3篇:手写促销策略引擎(满减、打折、立减)
java·设计模式·策略模式·开闭原则·编程实战·扩充系列·促销引擎
莫生灬灬6 小时前
DY联系人聊天Web面板支持获取联系人/聊天记录/发送消息
前端·chrome·websocket·c#
折哥的程序人生 · 物流技术专研8 小时前
第3篇:手写一个饮品制作模板(附代码)
java·设计模式·行为型模式·模版方法模式·钩子方法·代码复用·编程实战
逝水无殇8 小时前
C# 多态性详解
开发语言·后端·c#
逝水无殇8 小时前
C# 继承(Inheritance)详解
开发语言·后端·c#
ttod_qzstudio9 小时前
【软考设计模式】单例模式:唯一实例的管控与线程安全精讲
单例模式·设计模式
折哥的程序人生 · 物流技术专研10 小时前
第4篇:模板方法 vs 策略模式,面试怎么选?
java·设计模式·策略模式·行为型模式·模版方法模式·扩充系列·继承v组合
asdzx6711 小时前
C# 通过模板生成 Word 文档:文本与图片占位符替换完整攻略
开发语言·c#·word