依赖注入 理解Dependency Injection

参考:
控制反转 wiki
依赖注入参考1,能极大促进理解
依赖注入参考2

定义:

  • In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies.
    Inversion of Control(IOC):控制反转。 是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。
    其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,**对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递给它。**也可以说,依赖被注入到对象中。

  • Dependency: ClassA 中 含有 ClassB 的实例,就称ClassA对ClassB有一个依赖。

  • Dependency Injection: 直接在构造函数初始化是一种Hard init方式。可以在外部实例化ClassB后,传入ClassA中构造依赖关系。这种非自己主动初始化依赖,而通过外部来传入依赖的方式,就称为依赖注入

  • 硬初始化(hard init):这种在MovieLister中创建MovieFinderImpl的方式,使得MovieLister不仅仅依赖于MovieFinder这个接口,它还依赖于MovieListImpl这个实现。 这种在一个类中直接创建另一个类的对象的代码,和硬编码(hard-coded strings)以及硬编码的数字(magic numbers)一样,是一种导致耦合的坏味道,我们可以把这种坏味道称为硬初始化。

依赖注入的实现方式:

  1. 构造函数注入(Construction Injection)
public class MovieLister {
    private MovieFinder finder;
    
    public MovieLister(MovieFinder finder) {
        this.finder = finder;
    }
}
  1. setter注入
public class MovieLister {
    public void setFinder(MovieFinder finder) {
        this.finder = finder;
    }
}
  1. 接口注入
//1.首先要创建一个注入使用的接口。
public interface InjectFinder {
    void injectFinder(MovieFinder finder);
}
//2.我们让MovieLister实现这个接口。
class MovieLister implements InjectFinder {
    public void injectFinder(MovieFinder finder) {
      this.finder = finder;
    }
}
//3.我们需要根据不同的框架创建被依赖的MovieFinder的实现
Editorial Reviews Product Description Dependency Injection is an in-depth guide to the current best practices for using the Dependency Injection pattern-the key concept in Spring and the rapidly-growing Google Guice. It explores Dependency Injection, sometimes called Inversion of Control, in fine detail with numerous practical examples. Developers will learn to apply important techniques, focusing on their strengths and limitations, with a particular emphasis on pitfalls, corner-cases, and best practices. This book is written for developers and architects who want to understand Dependency Injection and successfully leverage popular DI technologies such as Spring, Google Guice, PicoContainer, and many others. The book explores many small examples of anchor concepts and unfolds a larger example to show the big picture. Written primarily from a Java point-of-view, this book is appropriate for any developer with a working knowledge of object-oriented programming in Java, Ruby, or C#. About the Author Dhanji R. Prasanna is an Enterprise Java consultant for technologies such as EJB3, JBI, JSF, Guice, Spring, HiveMind, and PicoContainer. He is a co-author of the Bean Validation (JSR-303), JAX-RS (JSR-311), Servlet 3.0 (JSR-315), and JavaServerFaces 2.0 (JSR-314) specifications. He is also co-author of the Java EE 6.0 (JSR-316) platform specification, which is the next edition of J2EE. Product Details * Paperback: 352 pages * Publisher: Manning Publications; 1 edition (August 28, 2009) * Language: English * ISBN-10: 193398855X * ISBN-13: 978-1933988559 * Product Dimensions: 9.1 x 7.4 x 0.8 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baiiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值