基於.net 職責鏈來實現 外掛程式模式

來源:互聯網
上載者:User

標籤:

    •   外掛程式式的例子
  1.  QQ電腦管家,有很多工具列表,點一下工具下載後就可以開始使用了 
  2.  eclipse ,X Server 等等
    •   外掛程式式的好處
  1. 外掛程式降低架構的複雜性,把擴充功能從架構中剝離出來
  2. 讓第三方有機會來擴充程式的功能
    •   思路

      公開一個外掛程式介面,如果.DLL或.EXE的代碼中有繼承這個介面就將其示為外掛程式,並將這些外掛程式放在同一目錄。運行程式的時候掃描目   錄並通過反射判斷.DLL或.EXE中是否存在該介面,若存在,則當作外掛程式載入進來。如示

         

  •   基於.net 職責鏈來實現 外掛程式模式

          1.定義命令介面

           

     public interface ICommand    {        ServerReturn execute();        ServerReturn Rollback();    }

       擷取目前的目錄下繼承該介面的方法

      

        public  List<ICommand> CommandList()        {            List<ICommand> ICommandList = new List<ICommand>();            string[] files = Directory.GetFiles(System.IO.Directory.GetCurrentDirectory());            int i = 0;            foreach (string file in files)            {                string ext = file.Substring(file.LastIndexOf("."));                if (ext != ".dll") continue;                try                {                    // 載入外掛程式                    Assembly tmp = Assembly.LoadFile(file);                    Type[] types = tmp.GetTypes();                    bool ok = false;                    foreach (Type t in types)                        if (IsValidCommand(t))                        {                            // 通過反射執行個體化                            ICommand plugin = (ICommand)tmp.CreateInstance(t.FullName);                            ICommandList.Add(plugin);                            ok = true;                            if (ok) break;                        }                }                catch (Exception err)                {                    throw err;                }            }            return ICommandList;        }        /// <summary>        /// 判斷DLL中是否繼承了ICommand介面        /// </summary>        /// <param name="t"></param>        /// <returns></returns>        private static bool IsValidCommand(Type t)        {            bool ret = false;            Type[] interfaces = t.GetInterfaces();            foreach (Type theInterface in interfaces)            {                if (theInterface.FullName == "ClassDemo.ICommand")                {                    ret = true;                    break;                }            }            return ret;        }

        職責鏈執行方法組

 

        /// <summary>        /// 方法執行        /// </summary>        public void exec()        {            List<ICommand> list = new List<ICommand>();            foreach( ICommand demo in list)            {                if(!demo.execute().isSurccess)                {                    demo.Rollback();                    return;                }            }        }

 


  

 

 

      

 

 

   

基於.net 職責鏈來實現 外掛程式模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.