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对象,添加了打开文件和保存文件的命令。然后我们依次执行这些命令,并在最后撤销执行的命令。运行代码后,你将看到打开文件、关闭文件、保存文件和撤销保存文件的输出。

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

相关推荐
Ray Liang17 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
七月丶20 小时前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞20 小时前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼21 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟2 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder2 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室2 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦3 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530144 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net