行为型-命令模式

一、简介:

命令模式(Command Pattern)是一种行为型设计模式,它的主要目的是将一个请求或操作封装成一个对象,从而允许你参数化客户端对象,并以不同的请求来进行操作和排队请求。命令模式将请求的发送者(调用者)和接收者(执行者)解耦,同时允许你支持撤销操作和事务。

以下是命令模式的关键参与者和工作原理:

  1. 命令(Command):命令是一个抽象类或接口,它定义了执行特定操作的方法(通常称为execute),以及可能需要的参数。具体的命令类将实现这个接口,并提供具体的操作和参数。

  2. 具体命令(Concrete Command):具体命令是命令接口的实现,它包含了实际执行操作的代码,以及操作需要的参数。

  3. 调用者(Invoker):调用者是发送命令的对象,它包含了一个命令对象,并在需要时触发命令的执行。调用者不需要知道命令的具体实现,只需要知道如何触发命令执行。

  4. 接收者(Receiver):接收者是实际执行命令操作的对象。命令对象会将操作委托给接收者来执行。接收者知道如何执行命令的具体操作。

  5. 客户端(Client):客户端创建具体命令对象,并将命令对象传递给调用者。客户端也可以负责设置命令的参数。

命令模式的工作原理如下:

  • 客户端创建一个具体命令对象,并指定它要执行的操作和参数。
  • 将具体命令对象传递给调用者对象。
  • 调用者对象在需要时触发具体命令对象的执行方法(execute)。
  • 具体命令对象执行操作,并委托给接收者来完成。

命令模式的优点包括:

  1. 松耦合:命令模式将调用者和接收者解耦,使得它们不需要相互了解具体的实现细节,从而提高了系统的松耦合度。

  2. 可扩展性:可以轻松地添加新的命令类和接收者类,而无需修改现有的代码。

  3. 撤销操作:命令模式支持撤销操作,因为可以把一系列命令对象加入栈中,从而允许逆向操作。

  4. 事务支持:可以将多个命令组合成一个事务,以确保它们要么全部成功执行,要么全部失败。

命令模式常见的应用场景包括遥控器控制、队列任务处理、文本编辑器的撤销/恢复功能等需要将请求封装成对象并支持撤销操作的情况。

二、示例:

以下示例中,我们创建了两个具体命令类来执行添加文本和删除文本操作,每个具体命令类都支持executeundo操作。命令管理器用于执行命令并支持撤销操作。通过撤销操作,用户可以回退到之前的编辑状态,实现了命令模式的撤销功能。

using System;
using System.Collections.Generic;

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

// 具体命令类 - 添加文本
class AddTextCommand : ICommand
{
    private readonly TextEditor textEditor;
    private readonly string textToAdd;

    public AddTextCommand(TextEditor editor, string text)
    {
        textEditor = editor;
        textToAdd = text;
    }

    public void Execute()
    {
        textEditor.AddText(textToAdd);
    }

    public void Undo()
    {
        textEditor.DeleteText(textEditor.Text.Length - textToAdd.Length, textToAdd.Length);
    }
}

// 具体命令类 - 删除文本
class DeleteTextCommand : ICommand
{
    private readonly TextEditor textEditor;
    private readonly int position;
    private readonly string deletedText;

    public DeleteTextCommand(TextEditor editor, int pos, int length)
    {
        textEditor = editor;
        position = pos;
        deletedText = textEditor.Text.Substring(pos, length);
    }

    public void Execute()
    {
        textEditor.DeleteText(position, deletedText.Length);
    }

    public void Undo()
    {
        textEditor.InsertText(position, deletedText);
    }
}

// 接收者 - 文本编辑器
class TextEditor
{
    public string Text { get; private set; } = "";

    public void AddText(string text)
    {
        Text += text;
    }

    public void DeleteText(int position, int length)
    {
        Text = Text.Remove(position, length);
    }

    public void InsertText(int position, string text)
    {
        Text = Text.Insert(position, text);
    }
}

// 命令管理器
class CommandManager
{
    private readonly Stack<ICommand> history = new Stack<ICommand>();

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

    public void Undo()
    {
        if (history.Count > 0)
        {
            ICommand command = history.Pop();
            command.Undo();
        }
    }
}

class Program
{
    static void Main()
    {
        TextEditor editor = new TextEditor();
        CommandManager commandManager = new CommandManager();

        // 添加文本
        ICommand addCommand1 = new AddTextCommand(editor, "Hello, ");
        commandManager.Execute(addCommand1);
        Console.WriteLine(editor.Text);  // 输出:Hello, 

        // 再次添加文本
        ICommand addCommand2 = new AddTextCommand(editor, "World!");
        commandManager.Execute(addCommand2);
        Console.WriteLine(editor.Text);  // 输出:Hello, World!

        // 撤销操作
        commandManager.Undo();
        Console.WriteLine(editor.Text);  // 输出:Hello, 

        // 再次撤销操作
        commandManager.Undo();
        Console.WriteLine(editor.Text);  // 输出:(空)
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值