zoukankan      html  css  js  c++  java
  • .net 职责链来实现 插件模式

    .net 职责链来实现 插件模式

      • 插件式的例子
    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架构设计
  • 相关阅读:
    I can do more…
    在希望的田野上
    卓越管理培训笔记
    Python 学习小计
    谈谈“直抒己见”
    [更新]关于博客园不支持RSS全文输出的解决方案
    效率生活二三事
    个人阅读解决方案
    Oracle函数sys_connect_by_path 详解
    基础班-模板配置
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4252587.html
Copyright © 2011-2022 走看看