首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# interface 的了解(工厂模式)简单实例(入门)

2012-11-05 
C# interface 的理解(工厂模式)简单实例(入门)C# interface定义:就是以前使用的类似于 API 的东西,别人告

C# interface 的理解(工厂模式)简单实例(入门)

C# interface定义:就是以前使用的类似于 API 的东西,别人告诉你一个类型,你在心得开发过程中可以使用。比如:

?

  interface ITest  {        string iText();  }  class Test:ITest  {    #region ITest Members(成员)             public string iText()             {                    // TODO:  Add Test.printText implementation                    return ("Test string.");             }    #endregion   }  class Test2:ITest  {   #region ITest Members            public string iText()            {                    // TODO:  Add Test.printText implementation                    return ("Test2 string.");            }   #endregion  }  class Factory   {            public static ITest create(int itype)            {                    if(itype==1)                    {                           return new Test();                    }                    else                    {                           return new Test2();                    }            }  }  private void button1_Click(object sender, System.EventArgs e)  {        ITest it=Factory.create(2);        this.label1.Text=it.iText();  }

Test 和 Test2 都是继承接口 ITest ,在使用ITest时候,使用了简单的Factory模式来建立,本来是使用了Rose来画一个UML模型上来也许讲解的更详细,但是Rose也是这次学习的一点,所以没有使用会,正在研究中.

一个简单的实例,让我了解到:

1、接口 Interface : 并不是我想象的那么可怕,如果我简单的理解就是一个户口登记的地方,在这里登记的用户(方法),在他的儿子(实现接口的类型: Test ,Test2)中,就必须承认Interface中的人员的存在,并且必须给安排一个位置(实现接口的内容)。所以接口的最简单的好处就是:保持了继承型,使更多的人联系起来。

2、工厂模式:Facory Model:最开始接触这些东西是在Patterns In Java 的pdf中看到的,因为模式的编程方式是对接口编程的,所以开始理解这些方面的时候理解上有问题了。现在总算能明白一点点了。工厂模式就是(ITest)的新生儿(接口的实现类: Test,Test2)的户口登记处,到时候不管你要用那个儿子,只需要在这注册一下,就ok了。

简单的一个实例,让我对接口interface和Factory Model有了最基本的认识。

?

热点排行