posts - 124,  comments - 29,  trackbacks - 0
 

接口   

接口是插件行为的描述。 接口不包括任何业务逻辑实现,业务逻辑包含在实现接口的类中。   

接口声明:

public interface IPlus

    {

        ///<summary>

        ///插件行为方法

        ///</summary>

        void DoSomething();

    }

 

     类的实现:

     public class Plus1: IPlus

    {

        #region IPlus成员

        void IPlus.DoSomething()

        {

            MessageBox.Show(GetType().ToString() + " Do somthing...");

        }

        #endregion

    }

插件化系统中提倡通过接口的方式来访问对象,也可以使用虚基类来实现整个系统。使用接口和使用类,在业界也是经常引起争议的话题,笔者比较推荐用接口来实现。    

反射   

反射主要理解为在未能引用类的类型定义情况下创建和访问对象的能力。

    名字空间Kaitu.System中包含了Form类。 一般情况下当需要创建一个Form类的对象时, 必须引用Kaitu.System命名空间, 然后声明Form类型的对象,然后用new操作创建对象。

    Form frm = new Form();   //一般类对象的创建。

    假如Form类的定义暂时不能引用到当前的开发环境下 但时程序在运行期却是能创建并访问它, 这个时候就要利用反射机制来创建该对象:

比如:

using System.Reflection;

//加载运行期的程序集。

Assembly SampleAssembly = Assembly.LoadFile(@"c:\kaitulib.dll");  

//创建程序集中包含的类对象。

object instance = SampleAssembly.CreateInstance("Guost.Lib.PlusA");

首先是通过程序集的文件名加载程序集对象, 这里类似设计期对程序集的引用。然后是通过类的名称字符串创建类对象,从这里可以看出,创建一个类对象不再用new的方式,而是通过程序集名称和类名字符串创建的。 这里可能有人要问,既然不引用程序集,那么怎样访问创建后的对象呢?用接口,反射创建的类对象都实现了接口,而接口我们在应用程序设计期是可见的。

IPlus myPlusObject = instance as IPlus; //将对象转到接口

MyPlusObject.DoSomething(); //调用业务逻辑

 1using System.Data;
 2using System.IO;
 3using System.Drawing;
 4using MyInterface;
 5using System.Text;
 6using System.Windows.Forms;
 7using System.Reflection;
 8
 9namespace pathTest
10{
11    public partial class Form1 : Form
12    {
13        public Form1()
14        {
15            InitializeComponent();
16        }

17        private Dictionary<String, inter> plusList = new Dictionary<string, inter>();
18        private void Form1_Load(object sender, EventArgs e)
19        {
20            找路径
24            创建目录
27            遍历目录所有文件提取目的dll中的类型
62
63        }

64
65        private void listBox1_Click(object sender, EventArgs e)
66        {
67            string key = listBox1.Items[this.listBox1.SelectedIndex].ToString();
68            inter myinter = plusList[key];
69            textBox2.Text = myinter.Show();
70        }

71    }

72}

以下是两个dll. 一个是MyInterface.dll, 一个是TestLib.dll.其中MyInterface.dll是主程序框架中写好后编译成dll后暴露给插件编程者的,插件编程者引用该dll.另一个TestLib.dll.以插件的形式给出,不需要程序里引用,通过程序集对象的CreatInstance( classname )方法来创建对象实例,创建好的对象实例转到接口然后调用接口中的行为。

bject plus = SampleAssembly.CreateInstance(objType[i].ToString());

 inter myinter = plus as inter;

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5
 6namespace MyInterface
 7{
 8    public interface inter
 9    {
10        String Show();
11    }

12}

 

 

 

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using MyInterface;
 5
 6namespace testLib
 7{
 8    public class Family:inter
 9    {
10        String motherName = "miaomiao";
11        String fatherName = "fox";
12        String sisterName = "big cat";
13        String myselfName = "hehe";
14
15        public String MotherName
16        {
17            get return motherName; }
18            set { motherName = value; }
19        }

20        public String FatherName
21        {
22            get return fatherName; }
23            set { fatherName = value; }
24        }

25        public String SisterName
26        {
27            get return sisterName; }
28            set { sisterName = value; }
29        }

30        public String MyselfName
31        {
32            get return myselfName; }
33            set { myselfName = value; }
34        }

35       
36        public Family()
37        { }
38        String  inter.Show()
39        {
40            String res = "Mother:" + motherName + ",Father:" + fatherName + ",Sister:" + sisterName + ",And Me " + myselfName;
41            return res;
42        }

43    }

44
45    public class BFriend:inter
46    {
47        String bfName = "tutu";
48        String myName = "miaomiao";
49        public String BfName
50        {
51            get return bfName; }
52            set { bfName = value; }
53        }

54        public String MyName
55        {
56            get return myName; }
57            set { myName = value; }
58        }

59        public BFriend()
60        {
61           
62        }

63        String inter.Show()
64        {
65            String res = bfName + " like " + myName + ",and " + myName + " like " + bfName;
66            return res;
67        }

68    }

69}

70
posted on 2008-10-29 17:53 天书 阅读(2641) 评论(1)  编辑 收藏 引用

FeedBack:
# re: 插件化开发——接口和反射[未登录]
2015-08-15 23:01 | x
开源插件化开发平台JXADF 功能特点:

1、基于OSGi、J2EE

2、插件集市 http://osgi.jxtech.net">http://osgi.jxtech.net">http://osgi.jxtech.net">http://osgi.jxtech.net 中有丰富的插件。

3、前端代码(界面、CSS、JS、图片等)、后端代码(JAVA业务逻辑、SQL脚本【只需要提供一种数据库脚本,在安装时会根据数据库类型自动转换】)全部打包在一个Bundle(JAR)中,分发、测试、积累、部署都十分方便。

4、支持热部署、多数据库、换肤、国际化等。

5、支持多种工流引擎,包括:健新科技自己研发的工作流引擎、Activiti、Oracle BPM,可根据需要集成自己的工作流引擎。

6、桌面端、移动端自己适应,开发一次,即可支持多种终端。

7、学习成本低、效率高。

8、有丰富的文档,参见:http://osgi.jxtech.net">http://osgi.jxtech.net">http://osgi.jxtech.net">http://osgi.jxtech.net

9、演示地址:http://demo.jxtech.net 用户名:admin 密码:123456

------------------------------------
官网:http://osgi.jxtech.net">http://osgi.jxtech.net">http://osgi.jxtech.net">http://osgi.jxtech.net
QQ群:429895245  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜