依赖注入

github https://github.com/spring2go/core-spring-patterns.git

依赖倒置原则 (Dependency Inversion Principle)
  • SOLID面向对象原理之一
    • 高层模块不应该依赖底层模块。两者都应该依赖于抽象
    • 抽象不应该依赖于细节。细节应该依赖于抽象。
  • 总结:就是面向接口编程
只要接口不变,就可以任意更换设备
  1. 主机就是高层模块,剥露很多接口,接口就是抽象
  2. 接口不依赖于设备,设备依赖于接口,不依赖于主机。

163d5a7ee7594bd0?w=1448&h=818&f=png&s=1128232

问题:高层模块依赖于底层模块,不利于扩展,如果扩展需要修改代码
  1. 如果止时,不发送日志了,需要发送短信,此时就需要修改代码
// 高层模块
public class AppMonitorNoDIP {

    // 负责将事件日志写到日志系统
    private EventLogWriter writer = null;
    
    // 应用有问题时该方法将被调用
    public void notify(String message) {
        if (writer == null) {
            writer = new EventLogWriter();
        }
        writer.write(message);
    }
    
    public static void main(String[] args) {
        AppMonitorNoDIP appMonitor = new AppMonitorNoDIP();
        appMonitor.notify("App has a problem ...");
    }
}

// 底层模块
class EventLogWriter {
    public void write(String message) {
        // 写到事件日志
        System.out.println("Write to event log, message : " + message);
    }
}
使用依赖倒置原则改进 关系图

163d5b81910d8321?w=1160&h=491&f=png&s=34876

  • 高层模块不应该依赖底层模块。两者都应该依赖于抽象。
  • 抽象不应该依赖于细节。细节应该依赖于抽象。
// 事件通知器接口
public interface INotifier {
    public void notify(String message);
}

// 发送短消息
public class SMSSender implements INotifier {
    public void notify(String message) {
        System.out.println("Send SMS, message : " + message);
    }
}

// 写到事件日志
public class EventLogWriter implements INotifier {
    public void notify(String message) {
        System.out.println("Write to event log, message : " + message);
    }
}

// 发送Email
public class EmailSender implements INotifier {
    public void notify(String message) {
        System.out.println("Send email, message : " + message);
    }
}
依赖倒置DIP 客户端调用
  • 高层模块不依赖于底层接口,依赖于抽象
public class AppMonitorIOC {
    // 事件通知器
    private INotifier notifier = null;
    
    // 应用有问题时该方法被调用
    public void notify(String message) {
        if (notifier == null) {
            // 将抽象接口映射到具体类
            notifier = new EventLogWriter(); (这里有耦合性,需要new出来)
        }
        notifier.notify(message);
    }
    
    public static void main(String[] args) {
        AppMonitorIOC appMonitor = new AppMonitorIOC();
        appMonitor.notify("App has a problem ...");
    }
}
控制反转( Inversion of Control)
  1. 传统做法: 主程序的依赖和装配都是由主程序驱动的
  2. 依赖注入做法:当需要依赖对象时,由依赖注入器运行期注入进来

163d5c5dd71d0bb5?w=1067&h=591&f=png&s=179709

依赖注入(Dependency Injection)具体实现
  • 构造函数注入
  • Setter注入
  • 接口注入
构造函数注入
public class AppMonitorConstructorInjection {
    // 事件通知器
    private INotifier notifier = null;
    
    public AppMonitorConstructorInjection(INotifier notifier) {
        this.notifier = notifier;
    }
    
    // 应用有问题时该方法被调用
    public void notify(String message) {
        notifier.notify(message);
    }
    
    public static void main(String[] args) {
        EventLogWriter writer = new EventLogWriter();
        AppMonitorConstructorInjection monitor = 
                new AppMonitorConstructorInjection(writer);
        monitor.notify("App has a problem ...");
    }
    
}
spring ioc 依赖注入
  1. 定义元数据运行时的依赖关系
  2. spring ioc 在运行期,将需要的依赖注入进来

163d5c794aad31c4?w=1298&h=815&f=png&s=373907

好处
  • 依赖解耦
    • 模块化:不只直接new
    • 易于测试
    • 易于变化和扩展

转载于:https://www.cnblogs.com/zhangjianbin/p/9148117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值