选择显示字体大小

介绍 ioc

<span class=postbody><span style=&quot;font-weight: bold&quot;>一、什么是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=&quot;http://xglw.51.net/5team/image/csdn_dev_image_2004-2-16115390.gif&quot;> <br><br>在我们中国,常见的mm与gg的认识方式有以下几种 <br><br>1 青梅竹马; 2 亲友介绍; 3 父母包办 <br><br>那么哪一种才是最好呢? <br><br>青梅竹马:girl从小就知道自己的boy。 <br><br><img border=0 src=&quot;http://xglw.51.net/5team/image/csdn_dev_image_2004-2-16115392.gif&quot;> <br><br></span>
<table align=center border=0 cellpadding=3 cellspacing=1 width=&quot;90%&quot;>
<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=&quot;http://xglw.51.net/5team/image/csdn_dev_image_2004-2-16115394.gif&quot;> <br><br><br><br></span>
<table align=center border=0 cellpadding=3 cellspacing=1 width=&quot;90%&quot;>
<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=&quot;http://xglw.51.net/5team/image/csdn_dev_image_2004-2-16115396.gif&quot;> <br><br><br></span>
<table align=center border=0 cellpadding=3 cellspacing=1 width=&quot;90%&quot;>
<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>我们知道好莱坞原则:&ldquo;do not call us, we will call you.&rdquo; 意思就是,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=&quot;http://xglw.51.net/5team/image/csdn_dev_image_2004-2-16115398.gif&quot;> <br>eric gamma说,要面向抽象编程。面向接口编程是面向对象的核心。 <br><br>组件应该分为两部分,即 <br><br>service, 所提供功能的声明 <br><br>implementation, service的实现 <br><br>好处是:多实现可以任意切换,防止 &ldquo;everything depends on everything&rdquo; 问题.即具体依赖于具体。 <br><br>所以,我们的boy应该是实现kissable接口。这样一旦girl不想kiss可恶的boy的话,还可以kiss可爱的kitten和慈祥的grandmother。 <br><br><img border=0 src=&quot;http://xglw.51.net/5team/image/csdn_dev_image_2004-2-161153910.gif&quot;> <br><br><br><span style=&quot;font-weight: bold&quot;>二、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=&quot;90%&quot;>
<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=&quot;90%&quot;>
<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(&ldquo;kissable&rdquo;); <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=&quot;90%&quot;>
<tbody>
<tr>
<td><span class=genmed><b>代码:</b></span></td></tr>
<tr>
<td class=code><container><br>&nbsp; &nbsp; <component class='&ldquo;boy&quot;' name=&quot;&ldquo;kissable&ldquo;&quot;>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp;<configuration> &hellip; </configuration><br>&nbsp; &nbsp; </component><br><br>&nbsp; &nbsp; <component class='&ldquo;girl&quot;' name='&ldquo;girl&quot;'></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=&quot;90%&quot;>
<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=&quot;90%&quot;>
<tbody>
<tr>
<td><span class=genmed><b>代码:</b></span></td></tr>
<tr>
<td class=code><beans><br>&nbsp; &nbsp; <bean class='&ldquo;boy&quot;/' id='&ldquo;boy&quot;'><br>&nbsp; &nbsp; <bean class='&ldquo;girl&quot;' id=&ldquo;girl&ldquo;><br>&nbsp; &nbsp; &nbsp; &nbsp; <property name='&ldquo;kissable&quot;'><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ref bean='&ldquo;boy&quot;/'> <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=&quot;90%&quot;>
<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=&quot;90%&quot;>
<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=&quot;font-weight: bold&quot;>参考资料</span> <br><br><br>1 本文主要插图及文字来源于thoughtworks公司的jon tirs&eacute;n 与 aslak helles&oslash;y(picocontainer的两位开发者),2003年在java polis的演讲ppt。有删改。 <br><br><a href=&quot;http://www.picocontainer.org/presentations/javapolis2003.ppt&quot; target=_blank>http://www.picocontainer.org/presentations/javapolis2003.ppt</a> <br><br><a href=&quot;http://www.picocontainer.org/presentations/javapolis2003.pdf&quot; target=_blank>http://www.picocontainer.org/presentations/javapolis2003.pdf</a> <br><br><br><br>2 dip, robert c martin, bob大叔的优秀论文 <br><br><a href=&quot;http://www.objectmentor.com/resources/articles/dip.pdf&quot; 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=&quot;http://www.martinfowler.com/articles/injection.html&quot; target=_blank>http://www.martinfowler.com/articles/injection.html</a> <br><br><br><br>4 ioc框架 <br><br>picocontainer 优秀的ioc框架 <br><br><a href=&quot;http://picocontainer.org/&quot; target=_blank>http://picocontainer.org/</a> <br><br>avalon <br><br><a href=&quot;http://avalon.apache.org/&quot; target=_blank>http://avalon.apache.org/</a> <br><br>spring framework <br><br><a href=&quot;http://www.springframework.org/&quot; target=_blank>http://www.springframework.org/</a> <br><br>hivemind <br><br><a href=&quot;http://jakarta.apache.org/commons/hivemind&quot; 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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons