程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 命令模式(Command Pattern)

設計模式(C#) - 命令模式(Command Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

將一個請求封裝為一個對象,從而使你可用不同的請求對客戶進行參數化;對請求排隊或記錄請求日志,以及支持可取消的操作。

示例

有一個Message實體類,某個類對它的操作有Insert()和Delete()方法。現在要求可以對之前的所有操作做撤銷和重復。

MessageModel

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

namespace Pattern.Command
{
  /**//// <summary>
  /// Message實體類
  /// </summary>
  public class MessageModel
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="msg">Message內容</param>
    /// <param name="pt">Message發布時間</param>
    public MessageModel(string msg, DateTime pt)
    {
      this._message = msg;
      this._publishTime = pt;
    }

    private string _message;
    /**//// <summary>
    /// Message內容
    /// </summary>
    public string Message
    {
      get { return _message; }
      set { _message = value; }
    }

    private DateTime _publishTime;
    /**//// <summary>
    /// Message發布時間
    /// </summary>
    public DateTime PublishTime
    {
      get { return _publishTime; }
      set { _publishTime = value; }
    }
  }
}

Action

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

namespace Pattern.Command
{
  /**//// <summary>
  /// enum
  /// 定義操作的兩種方法Insert和Delete
  /// </summary>
  public enum Action
  {
    /**//// <summary>
    /// Insert
    /// </summary>
    Insert,

    /**//// <summary>
    /// Delete
    /// </summary>
    Delete
  }
}

SqlMessage

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

namespace Pattern.Command
{
  /**//// <summary>
  /// 接收者(Receiver)角色
  /// Sql方式操作Message
  /// </summary>
  public class SqlMessage
  {
    /**//// <summary>
    /// 操作
    /// </summary>
    /// <param name="action">操作的方法</param>
    /// <param name="mm">Message實體對象</param>
    public void Operation(Action action, MessageModel mm)
    {
      switch (action)
      {
        case Action.Insert :
          Insert(mm);
          break;
        case Action.Delete :
          Delete(mm);
          break;
      }
    }

    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    private void Insert(MessageModel mm)
    {
      // 代碼略
    }

    /**//// <summary>
    /// 刪除Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    private void Delete(MessageModel mm)
    {
      // 代碼略
    }
  }
}

ICommand

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

namespace Pattern.Command
{
  /**//// <summary>
  /// 命令(Command)角色
  /// </summary>
  public interface ICommand
  {
    /**//// <summary>
    /// 執行
    /// </summary>
    /// <returns>操作的方法及操作的信息</returns>
    string Execute();

    /**//// <summary>
    /// 取消執行
    /// </summary>
    /// <returns>操作的方法及操作的信息</returns>
    string UnExecute();
  }
}

SqlMessageCommand

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

namespace Pattern.Command
{
  /**//// <summary>
  /// 具體命令(ConcreteCommand)角色
  /// </summary>
  public class SqlMessageCommand : ICommand
  {
    /**//// <summary>
    /// 操作的方法
    /// </summary>
    private Action _action;

    /**//// <summary>
    /// Message實體對象
    /// </summary>
    private MessageModel _mm;

    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="action">操作的方法</param>
    /// <param name="mm">Message實體對象</param>
    public SqlMessageCommand(Action action, MessageModel mm)
    {
      this._action = action;
      this._mm = mm;
    }

    /**//// <summary>
    /// 執行
    /// </summary>
    /// <returns>操作的方法及操作的信息</returns>
    public string Execute()
    {
      new SqlMessage().Operation(_action, _mm);

      return _action.ToString() + ":" + _mm.Message;
    }

    /**//// <summary>
    /// 取消執行(調用一個方法,以決定取消執行的算法)
    /// </summary>
    /// <returns>操作的方法及操作的信息</returns>
    public string UnExecute()
    {
      _action = GetUndoAction(_action);
      new SqlMessage().Operation(_action, _mm);

      return _action.ToString() + ":" + _mm.Message;
    }

    /**//// <summary>
    /// 獲得取消執行的算法
    /// </summary>
    /// <param name="action">操作的方法</param>
    /// <returns></returns>
    private Action GetUndoAction(Action action)
    {
      Action undo;

      switch (action)
      {
        case Action.Insert :
          undo = Action.Delete;
          break;
        case Action.Delete :
          undo = Action.Insert;
          break;
        // 這句沒啥用
        default :
          undo = Action.Insert;
          break;
      }

      return undo;
    }
  }
}

Message

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

namespace Pattern.Command
{
  /**//// <summary>
  /// 請求者(Invoker)角色
  /// </summary>
  public class Message
  {
    /**//// <summary>
    /// 命令集合(保存每次操作)
    /// </summary>
    private List<ICommand> _listCommand = new List<ICommand>();

    /**//// <summary>
    /// 命令集合中當前要執行的命令的索引
    /// </summary>
    private int current = 0;

    /**//// <summary>
    /// 執行Sql
    /// </summary>
    /// <param name="action">操作的方法</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns>操作的方法及操作的信息</returns>
    public string Do(Action action, MessageModel mm)
    {
      string rtn = "";

      ICommand cmd = new SqlMessageCommand(action, mm);
      rtn = cmd.Execute();

      _listCommand.Add(cmd);
      current++;

      return rtn;
    }

    /**//// <summary>
    /// 撤銷
    /// </summary>
    /// <param name="levels">執行撤銷操作的次數</param>
    /// <returns>操作的方法及操作的信息(用空格分開多條記錄)</returns>
    public string Undo(int levels)
    {
      string rtn = "";

      for (int i = 0; i < levels; i++)
      {
        if (current > 0)
        {
          ICommand cmd = _listCommand[--current];
          rtn += cmd.UnExecute() + " ";
        }
      }

      return rtn;
    }

    /**//// <summary>
    /// 重復
    /// </summary>
    /// <param name="levels">執行重復操作的次數</param>
    /// <returns>操作的方法及操作的信息(用空格分開多條記錄)</returns>
    public string Redo(int levels)
    {
      string rtn = "";

      for (int i = 0; i < levels; i++)
      {
        if (current < _listCommand.Count - 1)
        {
          ICommand cmd = _listCommand[current++];
          rtn += cmd.UnExecute() + " ";
        }
      }

      return rtn;
    }
  }
}

client

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Pattern.Command;

public partial class Command : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    Message m = new Message();

    Response.Write("操作");
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第1條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第2條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第3條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第4條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Delete, new MessageModel("第2條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第5條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Delete, new MessageModel("第3條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第6條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(m.Do(Action.Insert, new MessageModel("第7條", DateTime.Now)));
    Response.Write("<br />");
    Response.Write("<br />");

    Response.Write("撤銷4次操作");
    Response.Write("<br />");
    Response.Write(m.Undo(4));
    Response.Write("<br />");
    Response.Write("<br />");

    Response.Write("重復2次操作");
    Response.Write("<br />");
    Response.Write(m.Redo(2));
    Response.Write("<br />");
    Response.Write("<br />");

    Response.Write("撤銷3次操作");
    Response.Write("<br />");
    Response.Write(m.Undo(3));
  }
}

運行結果

操作

Insert:第1條

Insert:第2條

Insert:第3條

Insert:第4條

Delete:第2條

Insert:第5條

Delete:第3條

Insert:第6條

Insert:第7條

撤銷4次操作

Delete:第7條 Delete:第6條 Insert:第3條 Delete:第5條

重復2次操作

Insert:第5條 Delete:第3條

撤銷3次操作

Insert:第3條 Delete:第5條 Insert:第2條

參考

http://www.dofactory.com/Patterns/PatternCommand.aspx

OK

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved