站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题介绍 IOC

介绍 IOC

摘要: 介绍 IOC
内容: 一、什么是IOC

IoC就是Inversion of Control,控制反转。在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制。这称为控制反转。

下面我们以几个例子来说明什么是IoC


假设我们要设计一个Girl和一个Boy类,其中Girl有kiss方法,即Girl想要Kiss一个Boy。那么,我们的问题是,Girl如何能够认识这个Boy?



在我们中国,常见的MM与GG的认识方式有以下几种

1 青梅竹马; 2 亲友介绍; 3 父母包办

那么哪一种才是最好呢?

青梅竹马:Girl从小就知道自己的Boy。




<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl {&nbsp;
&nbsp; &nbsp; void kiss(){
&nbsp; &nbsp; &nbsp; &nbsp;Boy boy = new Boy();
&nbsp; &nbsp; }
}</td></tr></table>

然而从开始就创建的Boy缺点就是无法在更换。并且要负责Boy的整个生命周期。如果我们的Girl想要换一个怎么办?(笔者严重不支持Girl经常更换Boy)

亲友介绍:由中间人负责提供Boy来见面






<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl {
&nbsp; &nbsp; void kiss(){
&nbsp; &nbsp; &nbsp; &nbsp;Boy boy = BoyFactory.createBoy();&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; }
}</td></tr></table>

亲友介绍,固然是好。如果不满意,尽管另外换一个好了。但是,亲友BoyFactory经常是以Singleton的形式出现,不然就是,存在于Globals,无处不在,无处不能。实在是太繁琐了一点,不够灵活。我为什么一定要这个亲友掺和进来呢?为什么一定要付给她介绍费呢?万一最好的朋友爱上了我的男朋友呢?



父母包办:一切交给父母,自己不用费吹灰之力,只需要等着Kiss就好了。





<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl {
&nbsp; &nbsp; void kiss(Boy boy){
&nbsp; &nbsp; &nbsp; &nbsp;// kiss boy&nbsp;
&nbsp; &nbsp; &nbsp; boy.kiss();
&nbsp; &nbsp; }
}</td></tr></table>


Well,这是对Girl最好的方法,只要想办法贿赂了Girl的父母,并把Boy交给他。那么我们就可以轻松的和Girl来Kiss了。看来几千年传统的父母之命还真是有用哦。至少Boy和Girl不用自己瞎忙乎了。

这就是IOC,将对象的创建和获取提取到外部。由外部容器提供需要的组件。



我们知道好莱坞原则:“Do not call us, we will call you.” 意思就是,You, girlie, do not call the boy. We will feed you a boy。



我们还应该知道依赖倒转原则即 Dependence Inversion Princinple,DIP


Eric Gamma说,要面向抽象编程。面向接口编程是面向对象的核心。

组件应该分为两部分,即

Service, 所提供功能的声明

Implementation, Service的实现

好处是:多实现可以任意切换,防止 “everything depends on everything” 问题.即具体依赖于具体。

所以,我们的Boy应该是实现Kissable接口。这样一旦Girl不想kiss可恶的Boy的话,还可以kiss可爱的kitten和慈祥的grandmother。




二、IOC的type

IoC的Type指的是Girl得到Boy的几种不同方式。我们逐一来说明。

IOC type 0:不用IOC


<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl implements Servicable {

&nbsp; &nbsp; private Kissable kissable;

&nbsp; &nbsp; public Girl() {
&nbsp; &nbsp; &nbsp; &nbsp; kissable = new Boy();
&nbsp; &nbsp; }

&nbsp; &nbsp; public void kissYourKissable() {
&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss();
&nbsp; &nbsp; }

}</td></tr></table>

Girl自己建立自己的Boy,很难更换,很难共享给别人,只能单独使用,并负责完全的生命周期。

IOC type 1,先看代码:


<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl implements Servicable {

&nbsp; &nbsp; Kissable kissable;

&nbsp; &nbsp; public void service(ServiceManager mgr) {
&nbsp; &nbsp; &nbsp; &nbsp; kissable = (Kissable) mgr.lookup(“kissable”);
&nbsp; &nbsp; }

&nbsp; &nbsp; public void kissYourKissable() {
&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss();
&nbsp; &nbsp; }

}</td></tr></table>

这种情况出现于Avalon Framework。一个组件实现了Servicable接口,就必须实现service方法,并传入一个ServiceManager。其中会含有需要的其它组件。只需要在service方法中初始化需要的Boy。

另外,J2EE中从Context取得对象也属于type 1。


它依赖于配置文件


<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;


&nbsp; &nbsp;
</td></tr></table>

IOC type 2:


<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl {

&nbsp; &nbsp; private Kissable kissable;

&nbsp; &nbsp; public void setKissable(Kissable kissable) {
&nbsp; &nbsp; &nbsp; &nbsp; this.kissable = kissable;
&nbsp; &nbsp; }

&nbsp; &nbsp; public void kissYourKissable() {
&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss();
&nbsp; &nbsp; }

}</td></tr></table>

Type 2出现于Spring Framework,是通过JavaBean的set方法来将需要的Boy传递给Girl。它必须依赖于配置文件。


<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp;

</td></tr></table>

IOC type 3:


<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>public class Girl {

&nbsp; &nbsp; private Kissable kissable;

&nbsp; &nbsp; public Girl(Kissable kissable) {
&nbsp; &nbsp; &nbsp; &nbsp; this.kissable = kissable;
&nbsp; &nbsp; }

&nbsp; &nbsp; public void kissYourKissable() {
&nbsp; &nbsp; &nbsp; &nbsp; kissable.kiss();
&nbsp; &nbsp; }

}</td></tr></table>

这就是PicoContainer的组件 。通过构造函数传递Boy给Girl

<table align=center border=0 cellPadding=3 cellSpacing=1 width="90%">

<tr>
<td>代码:</td></tr>
<tr>
<td class=code>
PicoContainer container = new DefaultPicoContainer();
container.registerComponentImplementation(Boy.class);
container.registerComponentImplementation(Girl.class);
Girl girl = (Girl) container.getComponentInstance(Girl.class);
girl.kissYourKissable();</td></tr></table>





参考资料


1 本文主要插图及文字来源于ThoughtWorks公司的Jon Tirsén 与 Aslak Helles&oslash;y(PicoContainer的两位开发者),2003年在Java Polis的演讲PPT。有删改。

http://www.picocontainer.org/presentations/JavaPolis2003.ppt

http://www.picocontainer.org/presentations/JavaPolis2003.pdf



2 DIP, Robert C Martin, Bob大叔的优秀论文

http://www.objectmentor.com/resources/articles/dip.pdf



3 Dependency Injection 依赖注射,Matrin Fowler对DIP的扩展

http://www.martinfowler.com/articles/injection.html



4 IOC框架

PicoContainer 优秀的IOC框架

http://picocontainer.org/

Avalon

http://avalon.apache.org/

Spring Framework

http://www.springframework.org/

HiveMind

http://jakarta.apache.org/commons/hivemind

本文由冰云完成,首发于CSDN,作者保留中文版权。
未经许可,不得使用于任何商业用途。
欢迎转载,但请保持文章及版权声明完整。
如需联络请发邮件:icecloud(AT)sina.com
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
↑返回目录
前一篇: 扩展JAAS
后一篇: 如何封锁您的(或打开别人的) Java 代码