Command模式的實踐

來源:互聯網
上載者:User

Command模式的實踐

主題

    文章將通過一個簡單的 Shell  庫來闡述 Command模式的一個實際應用。

如何使用 Shell 庫

    在開始之前,我們先來看看如何使用這個 Shell 庫。 解決方案結構如(圖1-1)

 

圖1-1 解決方案 

    從圖上可以看出,該 Shell 庫非常的簡單。其中 ICommand 介面定義了只定義了一個 Execute 方法,該方法的簽名見代碼清單 1-1.

代碼清單 1-1 

    /// <summary>執行命令並返回命令執行結果</summary>
    /// <param name="args">需要執行的命令</param>
    /// <param name="executeMessage">命令執行結果</param>
    void Execute(string[] args, ref string executeMessage);

 

    Command 項目中包含了實現了 ICommand 介面的具體類。 見代碼清單 1-2

代碼清單 1-2 

    public class AddUserCommand : ICommand
    {
        public void Execute(string[] args, ref string executeMessage)
        {
            if (args.Length != 3 || Share.IsHasAnySpace(args))
            {
                executeMessage = "AddUser 命令參數不正確!格式如下:AddUser userName passWord";
                return;
            }
            string userName = args[1];
            string passWord = args[2];
            BllFactory.BllFactory.GetInstance().User.AddUser(userName, passWord);
            executeMessage = string.Format("添加使用者 {0} 成功!", userName);
        }
    }

 

    該具體類由客戶負責建立。在代碼清單 1-2 中,AddUserCommand 類實現了 ICommand 介面。具體的 Execute  方法的實現是調用了BLL層的 AddUser 方法去完成了添加使用者的操作。

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.