<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 { <br> void kiss(){ <br> boy boy = new boy(); <br> } <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> void kiss(){ <br> boy boy = boyfactory.createboy(); <br> } <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> void kiss(boy boy){ <br> // kiss boy <br> boy.kiss(); <br> } <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> private kissable kissable; <br><br> public girl() { <br> kissable = new boy(); <br> } <br><br> public void kissyourkissable() { <br> kissable.kiss(); <br> } <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> kissable kissable; <br><br> public void service(servicemanager mgr) { <br> kissable = (kissable) mgr.lookup(“kissable”); <br> } <br><br> public void kissyourkissable() { <br> kissable.kiss(); <br> } <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> <component class='“boy"' name="“kissable“"> <br> <configuration> … </configuration><br> </component><br><br> <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> private kissable kissable; <br><br> public void setkissable(kissable kissable) { <br> this.kissable = kissable; <br> } <br><br> public void kissyourkissable() { <br> kissable.kiss(); <br> } <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> <bean class='“boy"/' id='“boy"'><br> <bean class='“girl"' id=“girl“><br> <property name='“kissable"'><br> <ref bean='“boy"/'> <br> </property><br> </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> private kissable kissable; <br><br> public girl(kissable kissable) { <br> this.kissable = kissable; <br> } <br><br> public void kissyourkissable() { <br> kissable.kiss(); <br> } <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ø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>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器