C#設計模式系列:命令模式(Command)


1、命令模式簡介

1.1>、定義

  命令模式的目的是解除命令發出者和接收者之間的緊密耦合關系,使二者相對獨立,有利於程序的並行開發和代碼的維護。命令模式的核心思想是將請求封裝為一個對象,將其作為命令發起者和接收者的中介,而抽象出來的命令對象又使得能夠對一系列請求進行操作,如對請求進行排隊,記錄請求日志以及支持可撤銷的操作等。

1.2>、使用頻率

   中高

2、命令模式結構

2.1>、結構圖

2.2>、參與者

  命令模式參與者:

  ◊ Command:命令抽象類,聲明一個執行操作的接口Execute,該抽象類並不實現這個接口,所有的具體命令都繼承自命令抽象類。

  ◊ ConcreteCommand

    ° 定義一個接收者對象與動作之間的請求綁定

    ° 調用Receiver的操作,實現Execute方法

  ◊ Invoker:命令的接收者,將命令請求傳遞給相應的命令對象,每個ConcreteCommand都是一個Invoker的成員

  ◊ Receiver:命令的接收者,知道如何實施與執行一個請求相關的操作

  ◊ Client:客戶端程序,創建一個具體命令對象並設定它的接收者

  Command對象作為Invoker的一個屬性,當點擊事件發生時,Invoker調用方法Invoke()將請求發送給ConcreteCommand,再由ConcreteCommand調用Execute()將請求發送給Receiver,Client負責創建所有的角色,並設定Command與Invoker和Receiver之間的綁定關系。

3、命令模式結構實現

  Receiver.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CommandPattern.Structural
{
    public class Receiver
    {
        public void Action()
        {
            Console.WriteLine("Called Receiver.Action()");
        }
    }
}

  Command.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CommandPattern.Structural
{
    public abstract class Command
    {
        protected Receiver receiver;

        public Command(Receiver receiver)
        {
            this.receiver = receiver;
        }

        public abstract void Execute();
    }
}

  ConcreteCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CommandPattern.Structural
{
    public class ConcreteCommand : Command
    {
        public ConcreteCommand(Receiver receiver)
            : base(receiver)
        {
        }

        public override void Execute()
        {
            receiver.Action();
        }
    }
}

  Invoker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CommandPattern.Structural
{
    public class Invoker
    {
        private Command _command;

        public void SetCommand(Command command)
        {
            this._command = command;
        }

        public void ExecuteCommand()
        {
            _command.Execute();
        }
    }
}

  Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DesignPatterns.CommandPattern.Structural;

namespace DesignPatterns.CommandPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Receiver receiver = new Receiver();
            Command command = new ConcreteCommand(receiver);
            Invoker invoker = new Invoker();

            invoker.SetCommand(command);
            invoker.ExecuteCommand();
        }
    }
}

  運行輸出:

Called Receiver.Action()
請按任意鍵繼續. . .

4、命令模式應用分析

  命令模式適用情形:

  1>、將用戶界面和行為分離,使兩者的開發可以並行不悖。

  2>、在需要指定、排列和執行一系列請求的情況下,適用命令模式。

  3>、支持修改日志。

  命令模式優點:

  1>、命令模式將調用操作對象和知道如何實現該操作對象的解耦。

  2>、在Command要增加新的處理操作對象很容易,可以通過創建新的繼承自Command的子類來實現。

  3>、命令模式可以和Memento模式結合使用,支持取消的操作。

  4>、支持日志、請求隊列和復合命令。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM