menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

插件框架(Plug-in Framework)

from: http://msdn.microsoft.com/asp.net/archive/default.aspx?pull=/library/en-us/dnaspp/html/pluginframework.asp 中文

WHY?
*To extend an application's functionality without the need to re-compile and distribute it to customers.
*To add functionality without requiring access to the original source code.
*The business rules for the application change frequently, or new rules are added frequently.

HOW?
*How do you find the plug-in from within the application?
*How does the plug-in know what data to handle?
*How do you activate this plug-in?

SUMMARY
*Create a shared interfaces library.
*Create custom plug-ins implementing the custom interfaces.
*Create context arguments to pass to the plug-ins.
*Create a section in your config file to hold plug-in names.
*Instantiate plug-ins using an IConfigurationSectionHandler implementer class.
*Call your plug-ins.
*Go home and spend some quality time away from your computer.

综述:
通常的插件框架的设计要包括平台(platform)和插件(Plug-in)两个部分。平台的功能通常包括软件的核心功能和插件的处理功能。而插件通常用来对平台功能的扩展与补充,可以集中管理,能够定义出标准接口。插件需要通过平台扩展接口获取主框架的各种资源和数据,可包括各种系统句柄,程序内部数据以及内存分配等。而平台则通过插件接口调用插件所实现的功能,读取插件处理数据等。这是Photoshop的插件设计方案。
当然也有纯粹的插件实现的软件,一个非常小的内核和其上的插件组成。这些插件包括界面,资源管理,帮助系统,以及其它插件使用的库等等。插件的耦合点通过XML中定义的已知接口(plugin.xml)或者OSGI格式的manifest文件,其扩展点的范围包括从正规表达式字符串,到一个类的描述。这是Eclipse的插件设计方案。

设计:
使用配置文件/注册表(Registry)的好处是可以非常详尽的设定Plug-in的功能,这样做的同时也损失了一部分灵活性,诚然也可以通过注册插件的方式来弥补这种灵活性,但是在插件中必须增加自我注册信息,同时在调用插件之前必须先注册该插件。IE中的BHO(Browser Helper Object)就是这种方案。
另一种方案是把所有的插件都指定放在特定路径下,然后遍历该目录下的文件,得到所有可用的插件。Foobar中的插件应该是这种方案。

实现:
插件框架的实现通常采用分离接口(Separated Interface)定义在不同环境下的行为,通过工厂方法(Factory Method)生成在相应环境下的实现。这种框架在使用反射(reflect)机制的语言(java, C#)中可以充分发挥其优势,而在不支持反射机制的语言中则需要添加一个条件判断以达到将接口映射到具体的实现的目的。

参考资料:
关于Plug-In实现的描述
Eclipse Platform SDK roadmap
Patterns of Enterprise Application Architecture

Update: 2005-09-07
Plug-in Architecture Framework for Beginners
An MFC extension library to enable DLL plug-in technology for your application using MESSAGE_MAPs
Plug-In framework using DLLs
Multi platform plug-in development made easy!
How to do run-time (or explicit) linking of C++ plug-in components and objects
Using a Doc/View exported from a dynamically loaded DLL
ATL COM Based Addin / Plugin Framework With Dynamic Toolbars and Menus
SkinX, A framework of a skin plug-in package
Pluggable Event Handler

哈哈,你被贴了!

http://spaces.msn.com/members/awkward-imitator/Blog/cns!1psDvSzsRoxWjHXPweBxx38w!147.entry

评论已关闭