到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.matrix.org.cn无关,不对该信息负责.通过http://www.matrix.org.cn//resource/article/0/890.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
介绍 IOC
作者:chris 来源:www.matrix.org.cn  发布时间:2006-02-22 17:48:02.967

<SPAN class=postbody><SPAN style="FONT-WEIGHT: bold">一、什么是IOC</SPAN> <BR><BR>IoC就是Inversion of Control,控制反转。在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制。这称为控制反转。 <BR><BR>下面我们以几个例子来说明什么是IoC <BR><BR><BR>假设我们要设计一个Girl和一个Boy类,其中Girl有kiss方法,即Girl想要Kiss一个Boy。那么,我们的问题是,Girl如何能够认识这个Boy? <BR><BR><IMG border=0 src="http://xglw.51.net/5team/image/CSDN_Dev_Image_2004-2-16115390.gif"> <BR><BR>在我们中国,常见的MM与GG的认识方式有以下几种 <BR><BR>1 青梅竹马; 2 亲友介绍; 3 父母包办 <BR><BR>那么哪一种才是最好呢? <BR><BR>青梅竹马:Girl从小就知道自己的Boy。 <BR><BR><IMG border=0 src="http://xglw.51.net/5team/image/CSDN_Dev_Image_2004-2-16115392.gif"> <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl {&nbsp; <BR>&nbsp; &nbsp; void kiss(){ <BR>&nbsp; &nbsp; &nbsp; &nbsp;Boy boy = new Boy(); <BR>&nbsp; &nbsp; } <BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>然而从开始就创建的Boy缺点就是无法在更换。并且要负责Boy的整个生命周期。如果我们的Girl想要换一个怎么办?(笔者严重不支持Girl经常更换Boy) <BR><BR>亲友介绍:由中间人负责提供Boy来见面 <BR><BR><IMG border=0 src="http://xglw.51.net/5team/image/CSDN_Dev_Image_2004-2-16115394.gif"> <BR><BR><BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl { <BR>&nbsp; &nbsp; void kiss(){ <BR>&nbsp; &nbsp; &nbsp; &nbsp;Boy boy = BoyFactory.createBoy();&nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; } <BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>亲友介绍,固然是好。如果不满意,尽管另外换一个好了。但是,亲友BoyFactory经常是以Singleton的形式出现,不然就是,存在于Globals,无处不在,无处不能。实在是太繁琐了一点,不够灵活。我为什么一定要这个亲友掺和进来呢?为什么一定要付给她介绍费呢?万一最好的朋友爱上了我的男朋友呢? <BR><BR><BR><BR>父母包办:一切交给父母,自己不用费吹灰之力,只需要等着Kiss就好了。 <BR><BR><IMG border=0 src="http://xglw.51.net/5team/image/CSDN_Dev_Image_2004-2-16115396.gif"> <BR><BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl { <BR>&nbsp; &nbsp; void kiss(Boy boy){ <BR>&nbsp; &nbsp; &nbsp; &nbsp;// kiss boy&nbsp; <BR>&nbsp; &nbsp; &nbsp; boy.kiss(); <BR>&nbsp; &nbsp; } <BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR>Well,这是对Girl最好的方法,只要想办法贿赂了Girl的父母,并把Boy交给他。那么我们就可以轻松的和Girl来Kiss了。看来几千年传统的父母之命还真是有用哦。至少Boy和Girl不用自己瞎忙乎了。 <BR><BR>这就是IOC,将对象的创建和获取提取到外部。由外部容器提供需要的组件。 <BR><BR><BR><BR>我们知道好莱坞原则:“Do not call us, we will call you.” 意思就是,You, girlie, do not call the boy. We will feed you a boy。 <BR><BR><BR><BR>我们还应该知道依赖倒转原则即 Dependence Inversion Princinple,DIP <BR><BR><IMG border=0 src="http://xglw.51.net/5team/image/CSDN_Dev_Image_2004-2-16115398.gif"> <BR>Eric Gamma说,要面向抽象编程。面向接口编程是面向对象的核心。 <BR><BR>组件应该分为两部分,即 <BR><BR>Service, 所提供功能的声明 <BR><BR>Implementation, Service的实现 <BR><BR>好处是:多实现可以任意切换,防止 “everything depends on everything” 问题.即具体依赖于具体。 <BR><BR>所以,我们的Boy应该是实现Kissable接口。这样一旦Girl不想kiss可恶的Boy的话,还可以kiss可爱的kitten和慈祥的grandmother。 <BR><BR><IMG border=0 src="http://xglw.51.net/5team/image/CSDN_Dev_Image_2004-2-161153910.gif"> <BR><BR><BR><SPAN style="FONT-WEIGHT: bold">二、IOC的type</SPAN> <BR><BR>IoC的Type指的是Girl得到Boy的几种不同方式。我们逐一来说明。 <BR><BR>IOC type 0:不用IOC <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl implements Servicable { <BR><BR>&nbsp; &nbsp; private Kissable kissable; <BR><BR>&nbsp; &nbsp; public Girl() { <BR>&nbsp; &nbsp; &nbsp; &nbsp; kissable = new Boy(); <BR>&nbsp; &nbsp; } <BR><BR>&nbsp; &nbsp; public void kissYourKissable() { <BR>&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss(); <BR>&nbsp; &nbsp; } <BR><BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>Girl自己建立自己的Boy,很难更换,很难共享给别人,只能单独使用,并负责完全的生命周期。 <BR><BR>IOC type 1,先看代码: <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl implements Servicable { <BR><BR>&nbsp; &nbsp; Kissable kissable; <BR><BR>&nbsp; &nbsp; public void service(ServiceManager mgr) { <BR>&nbsp; &nbsp; &nbsp; &nbsp; kissable = (Kissable) mgr.lookup(“kissable”); <BR>&nbsp; &nbsp; } <BR><BR>&nbsp; &nbsp; public void kissYourKissable() { <BR>&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss(); <BR>&nbsp; &nbsp; } <BR><BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>这种情况出现于Avalon Framework。一个组件实现了Servicable接口,就必须实现service方法,并传入一个ServiceManager。其中会含有需要的其它组件。只需要在service方法中初始化需要的Boy。 <BR><BR>另外,J2EE中从Context取得对象也属于type 1。 <BR><BR><BR>它依赖于配置文件 <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code><CONTAINER><BR>&nbsp; &nbsp; <component class='“Boy"' name="“kissable“">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp;<CONFIGURATION> … </CONFIGURATION><BR>&nbsp; &nbsp; </component><BR><BR>&nbsp; &nbsp; <component class='“Girl"' name='“girl"'></component><BR></CONTAINER></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>IOC type 2: <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl { <BR><BR>&nbsp; &nbsp; private Kissable kissable; <BR><BR>&nbsp; &nbsp; public void setKissable(Kissable kissable) { <BR>&nbsp; &nbsp; &nbsp; &nbsp; this.kissable = kissable; <BR>&nbsp; &nbsp; } <BR><BR>&nbsp; &nbsp; public void kissYourKissable() { <BR>&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss(); <BR>&nbsp; &nbsp; } <BR><BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>Type 2出现于Spring Framework,是通过JavaBean的set方法来将需要的Boy传递给Girl。它必须依赖于配置文件。 <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code><BEANS><BR>&nbsp; &nbsp; <BEAN class='“Boy"/' id='“boy"'><BR>&nbsp; &nbsp; <BEAN class='“Girl"' id=“girl“><BR>&nbsp; &nbsp; &nbsp; &nbsp; <property name='“kissable"'><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<REF bean='“boy"/'> <BR>&nbsp; &nbsp; &nbsp; &nbsp; </property><BR>&nbsp; &nbsp; </BEAN><BR></BEANS></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>IOC type 3: <BR><BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code>public class Girl { <BR><BR>&nbsp; &nbsp; private Kissable kissable; <BR><BR>&nbsp; &nbsp; public Girl(Kissable kissable) { <BR>&nbsp; &nbsp; &nbsp; &nbsp; this.kissable = kissable; <BR>&nbsp; &nbsp; } <BR><BR>&nbsp; &nbsp; public void kissYourKissable() { <BR>&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss(); <BR>&nbsp; &nbsp; } <BR><BR>}</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>这就是PicoContainer的组件 。通过构造函数传递Boy给Girl <BR></SPAN>
<TABLE align=center border=0 cellPadding=3 cellSpacing=1 width="90%">
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码:</B></SPAN></TD></TR>
<TR>
<TD class=code><BR>PicoContainer container = new DefaultPicoContainer(); <BR>container.registerComponentImplementation(Boy.class); <BR>container.registerComponentImplementation(Girl.class); <BR>Girl girl = (Girl) container.getComponentInstance(Girl.class); <BR>girl.kissYourKissable();</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR><BR><BR><BR><SPAN style="FONT-WEIGHT: bold">参考资料</SPAN> <BR><BR><BR>1 本文主要插图及文字来源于ThoughtWorks公司的Jon Tirsén 与 Aslak Helles&oslash;y(PicoContainer的两位开发者),2003年在Java Polis的演讲PPT。有删改。 <BR><BR><A href="http://www.picocontainer.org/presentations/JavaPolis2003.ppt" target=_blank>http://www.picocontainer.org/presentations/JavaPolis2003.ppt</A> <BR><BR><A href="http://www.picocontainer.org/presentations/JavaPolis2003.pdf" target=_blank>http://www.picocontainer.org/presentations/JavaPolis2003.pdf</A> <BR><BR><BR><BR>2 DIP, Robert C Martin, Bob大叔的优秀论文 <BR><BR><A href="http://www.objectmentor.com/resources/articles/dip.pdf" target=_blank>http://www.objectmentor.com/resources/articles/dip.pdf</A> <BR><BR><BR><BR>3 Dependency Injection 依赖注射,Matrin Fowler对DIP的扩展 <BR><BR><A href="http://www.martinfowler.com/articles/injection.html" target=_blank>http://www.martinfowler.com/articles/injection.html</A> <BR><BR><BR><BR>4 IOC框架 <BR><BR>PicoContainer 优秀的IOC框架 <BR><BR><A href="http://picocontainer.org/" target=_blank>http://picocontainer.org/</A> <BR><BR>Avalon <BR><BR><A href="http://avalon.apache.org/" target=_blank>http://avalon.apache.org/</A> <BR><BR>Spring Framework <BR><BR><A href="http://www.springframework.org/" target=_blank>http://www.springframework.org/</A> <BR><BR>HiveMind <BR><BR><A href="http://jakarta.apache.org/commons/hivemind" target=_blank>http://jakarta.apache.org/commons/hivemind</A> <BR><BR>本文由冰云完成,首发于CSDN,作者保留中文版权。 <BR>未经许可,不得使用于任何商业用途。 <BR>欢迎转载,但请保持文章及版权声明完整。 <BR>如需联络请发邮件:icecloud(AT)sina.com</SPAN>

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读