zoukankan      html  css  js  c++  java
  • 插件式设计开发心得(一)【文摘】

    转自:http://blog.csdn.net/cloudkurten/article/details/6474643

    本人对插件式应用框架的开发是相当喜欢的,记得曾经写过一篇文章,《将工厂模式升华为插件式框架》,这篇文章中写的是在控制台下实现的一种将工厂模式进一步加工后的结果。这两天又重新理了一下思路,于是决定动手写一个winform环境下的插件式框架开发。当然,现在插件式开发的软件也是相当多的,这个可以网上搜一下,或者看我的《将工厂模式升华为插件式框架》,最后有提到。

    插件式开发的核心思想我想应该是针对接口编程,那么好吧,我们现在开始插件式框架开发之旅吧。我以尝试开发一个小型系统工具为例子,以此为基础进行介绍我的插件式开发的思想,希望大家多多交流。

    插件式应用框架一般都有以下几部分组成:

    1、宿主程序:主界面程序(包含各种工具、菜单等),插件引擎(解析插件程序集),通信契约。

    2、插件

    3、另外的程序集、组件库(起框架辅助作用)

    好吧,现在我们开始设计我们自己的插件引擎吧。

    首先,我们要新建一个IApplication接口,此接口包含了主界面程序中所包含的所有内容,比如菜单,工具栏,tabcontrol,tabpage等。IApplication接口是所有插件与主主界面程序通信的公共契约,所以非常重要。代码如下:

    namespace WingOfDream.SystemTool.CoreFramework{
        using System.Windows.Forms;
        /// 宿主程序的所有属性
        public interface IApplication{
            /// 主程序标题
            string Caption { get; set; }
    
            /// 主程序当前使用的工具Tool名称
            string CurrentTool { get; set; }
    
            /// 主程序当前的ToolStrip容器
            ToolStripContainer CurrentToolStripContainer { get; set; }
    
            /// 当前的ToolStrip
            ToolStrip CurrentToolStrip { get; set; }
    
            /// 当前的菜单
            MenuStrip CurrentMenuStrip { get; set; }
    
            /// 主程序当前显示的TabControl
            TabControl CurrentTabControl { get; set; }
    
            /// 主程序当前显示的TabPage
            TabPage CurrentTabPage { get; set; }
    
            /// 主程序名称
            string Name { get; }
    
            /// 主程序窗体对象
            Form MainPlatform { get; set; }

    /// 主程序UI界面的Visible属性 bool Visible { get; set; } } }
     

    读者可以看到,IApplication接口中包含的仅仅是一些属性,而插件就是通过这些属性,与主界面进行交互的。

    下面来实现这个接口:

    namespace WingOfDream.SystemTool.CoreFramework{
        using System.Windows.Forms;
    
        /// 实现IApplication
        public class Application : IApplication{
            ///region IApplication 成员
            public string Caption{
                get{ return this.Caption; }
                set{ this.Caption = value; }
            }
    public string CurrentTool{ get{ return this.CurrentTool; } set{ this.CurrentTool = value; } }
    public ToolStripContainer CurrentToolStripContainer{ get{ return this.CurrentToolStripContainer; } set{ this.CurrentToolStripContainer = value; } }
    public MenuStrip CurrentMenuStrip{ get{ return this.CurrentMenuStrip;} set{ this.CurrentMenuStrip = value; } }
    public ToolStrip CurrentToolStrip{ get{ return this.CurrentToolStrip; } set{ this.CurrentToolStrip = value; } }
    public TabControl CurrentTabControl{ get{ return this.CurrentTabControl; } set{ this.CurrentTabControl = value; } }
    public TabPage CurrentTabPage{ get{ return this.CurrentTabPage; } set{ this.CurrentTabPage = value; } }
    public string Name { get { return this.Name; } }
    public Form MainPlatform{ get{ return this.MainPlatform; } set{ this.MainPlatform = value; } }
    public bool Visible{ get{ return this.Visible; } set{ this.Visible = value; } } } }
     

    要知道,接口一旦继承就必须实现。具体可以参看MSDN中关于接口跟抽象类的设计原则,或者《.NET设计规范》。

    有了主的通信契约,我们就可以定义插件类型的接口了。

    我们首先得定义一个IPlugin接口,该接口为所有插件的基接口,所有插件接口必须继承与该接口。代码如下:

    namespace WingOfDream.SystemTool.CoreFramework{   
        /// 所有框架插件接口的基接口     
        public interface IPlugin   {   
            /// 按钮显示的文字    
            string Caption { get; }   
     
            /// 按钮所属类别   
            string Category { get; }   
        }   
    }  
     

    下面我们来设计插件接口,首先请看IApplication接口中声明的插件对象的UI对象。我们要开发的插件UI对象有命令(ICommand)按钮、菜单栏(IMenuDef)、工具(ITool),工具条(IToolBarDef)、还有tabpage(ITabPage)。

  • 相关阅读:
    第36课 经典问题解析三
    第35课 函数对象分析
    67. Add Binary
    66. Plus One
    58. Length of Last Word
    53. Maximum Subarray
    38. Count and Say
    35. Search Insert Position
    28. Implement strStr()
    27. Remove Element
  • 原文地址:https://www.cnblogs.com/zhangqingsh/p/2980746.html
Copyright © 2011-2022 走看看