Ioc的推荐实现方式

Ioc(Inversion of Control)中文翻译为“控制反转”,Ioc的抽象概念是依赖关系转移,即“程序与实现必须依赖于抽象”。
 
控制反转最常见的实现方式就是依赖注入(Depedency Injection),依赖注入的含义是:保留抽象接口,让组件依赖抽象接口,当组件要与其他对象发生依赖关系时,通过抽象接口来注入依赖的实际对象。
 
在解说Ioc之前,先看一个设计的例子:
设计一个保存数据到软盘、USB设备的程序。
 
 
一、反面例子,没有使用Ioc
 
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:13:32
 * 
 * 
 */
public class FloppyWriter {
    public void saveToDevice() {
        System.out.println("FloppyWriter.saveToDevice(): Floppy!");
    }
}
 
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:14:25
 * 
 * 
 */
public class BusinessObject {
    private FloppyWriter floppyWriter;
 
    public FloppyWriter getFloppyWriter() {
        return floppyWriter;
    }
 
    public void setFloppyWriter(FloppyWriter floppyWriter) {
        this.floppyWriter = floppyWriter;
    }
 
    public void save() {
        System.out.println("----BusinessObjectsave()....");
        floppyWriter.saveToDevice();
        System.out.println("----BusinessObjectsave()....");
    }
}
 
 
 
 
 
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 1:15:17
 * 
 * 
 */
public class Client {
    public static void main(String args[]){
        //
        BusinessObject businessObject=new BusinessObject();
        FloppyWriter floppyWriter=new FloppyWriter();
        businessObject.setFloppyWriter(floppyWriter);
 
        //
        businessObject.save();
    }
}
 
 
 
 
 
 
 
 
二、Ioc的实现推荐实现方式:setter方式
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:38:32
 * 
 */
public interface IDeviceWriter {
    public void saveToDevice();
}
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:44:46
 * ,
 */
public class FloppyWriter implements IDeviceWriter{
    public void saveToDevice() {
        System.out.println("FloppyWriter.saveToDevice(): Floppy!");
    }
}
 
 
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:44:57
 * ,USB
 */
public class USBWriter implements IDeviceWriter{
    public void saveToDevice() {
        System.out.println("USBWriter.saveToDevice(): USB!");
    }
}
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:40:24
 * 
 */
public class BusinessObject {
    private IDeviceWriter writer;
 
    public IDeviceWriter getWriter() {
        return writer;
    }
 
    public void setWriter(IDeviceWriter writer) {
        this.writer = writer;
    }
 
    /**
     * 
     */
    public void save(){
        System.out.println("----BusinessObjectsave()....");
        writer.saveToDevice();
        System.out.println("----BusinessObjectsave()....");
    }
}
 
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:51:09
 * 
 */
public class Client {
    public static void main(String args[]){
        //
        IDeviceWriter floppyWriter=new FloppyWriter();
        IDeviceWriter usbWriter=new USBWriter();
 
        //
        BusinessObject businessObject1=new BusinessObject();
        BusinessObject businessObject2=new BusinessObject();
 
        //
        businessObject1.setWriter(floppyWriter);
        businessObject1.save();
 
        System.out.println();
 
        //
        businessObject2.setWriter(usbWriter);
        businessObject2.save();
    }
}
 
 
 
 
 
 
 
 
 
 
 
 
 
三、Ioc的实现推荐实现方式:constructor方式
 
 
 
 
 
 
setter方式基础上修改,IDeviceWriterFloppyWriterUSBWriter不变,BusinessObject
 
 
 
 
 
 
 
Client类修改为:
 
 
 
 
 
 
 
 
 
  
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:32:03
 * 
 */
public class BusinessObject {
    private IDeviceWriter writer;
 
    public BusinessObject() {
    }
 
    public BusinessObject(IDeviceWriter writer) {
        this.writer = writer;
    }
 
    public IDeviceWriter getWriter() {
        return writer;
    }
 
    public void setWriter(IDeviceWriter writer) {
        this.writer = writer;
    }
 
    /**
     * 
     */
    public void save() {
        System.out.println("----BusinessObjectsave()....");
        writer.saveToDevice();
        System.out.println("----BusinessObjectsave()....");
    }
}
 
 
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-9-25
 * Time: 11:35:52
 * 
 */
public class Client {
    public static void main(String args[]){
        //
        IDeviceWriter floppyWriter=new FloppyWriter();
        IDeviceWriter usbWriter=new USBWriter();
 
        //
        BusinessObject businessObject1=new BusinessObject(floppyWriter);
        BusinessObject businessObject2=new BusinessObject(usbWriter);
 
        //
        businessObject1.save();
        System.out.println();
        businessObject2.save();
    }
}
 
 
 
两种实现方式执行的结果相同:
 
----开始调用业务组件BusinessObject的save()方法....
调用FloppyWriter.saveToDevice()方法: 保存到了Floppy设备上!
----结束调用业务组件BusinessObject的save()方法....
 
----开始调用业务组件BusinessObject的save()方法....
调用USBWriter.saveToDevice()方法: 保存到了USB设备上!
----结束调用业务组件BusinessObject的save()方法....
 
Process finished with exit code 0


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/43900,如需转载请自行联系原作者
上一篇:(十二)java嵌套类和内部类


下一篇:NHibernate初探(七) 一对多关系延迟机制