首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 設計模式:命令模式請求者與歷史管理 (4/5)

     編輯:關於C語言

請求者:Message (Invoker)

封裝命令執行、撤銷、重做邏輯,維護命令歷史:

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

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

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

        /// 
        /// 執行 Sql
        /// 
        /// 操作的方法
        /// Message 實體物件
        /// 操作的方法及操作的信息
        public string Do(Action action, MessageModel mm)
        {
            string rtn = "";

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

            _listCommand.Add(cmd);
            current++;

            return rtn;
        }

        /// 
        /// 撤銷
        /// 
        /// 執行撤銷操作的次數
        /// 操作的方法及操作的信息(用空格分開多條記錄)
        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;
        }

        /// 
        /// 重復
        /// 
        /// 執行重復操作的次數
        /// 操作的方法及操作的信息(用空格分開多條記錄)
        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;
        }
    }
}

Invoker 核心機制

  • 命令歷史:_listCommand 記錄所有執行過的命令。
  • 當前指標:current 指向下一個要執行/重做的命令位置。
  • 執行流程:Do() 建立命令 → Execute() → 加入歷史 → 指標前進。
  • 撤銷流程:Undo() 指標後退 → 取得命令 → UnExecute()(反向操作)。
  • 重做流程:Redo() 取得當前命令 → UnExecute() → 指標前進。

注意:Redo 邏輯疑似問題

// 原始碼中 Redo 呼叫 UnExecute(),通常應呼叫 Execute() 重新執行
ICommand cmd = _listCommand[current++];
rtn += cmd.UnExecute() + " ";  // 疑似應為 cmd.Execute()

標準命令模式中 Redo 應重新執行命令(Execute()),而非執行反向操作。此處可能為示例簡化或邏輯差異。

命令模式四大角色對應(前四篇)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
模式角色本例類別說明
CommandICommand命令介面
ConcreteCommandSqlMessageCommand具體命令,封裝 Receiver 與 Action
ReceiverSqlMessage接收者,執行實際操作
InvokerMessage請求者,管理命令歷史與撤銷重做