Software development--Dependency Injection

Source: Internet
Author: User

Control inversion (inversion of controls, abbreviated to IOC) is a design principle for object-oriented programming that can be used to reduce the degree of coupling between computer code. The most common way is called Dependency injection (Dependency injection, short di). There is also a way to call "dependency lookup" (Dependency lookup). By controlling the inversion, the object

When created, an external entity that regulates all objects within the system is passed to it by reference to the object on which it depends. It can also be said that the dependency is injected into the object.

Class A uses object B of Class B, and in general, you need to explicitly type a B object in the code of a.

With the dependency injection technique, the code of a only needs to define a private B object, does not need a direct new to obtain the object, but through the relevant container control program to the external new and inject the B object into the Class A reference. The state specified by the configuration file (such as XML) is used when the method is obtained and the object is fetched.

Dependency injection is implemented in the following way:
Based on the interface. Implements a specific interface for an external container to inject an object of the dependent type.
Based on the set method. The public set method that implements a specific property lets the outer container invoke an object that is passed in to the dependent type.
Based on the constructor function. A constructor that implements a specific parameter, passing in an object of the dependent type when a new object is created.

1. Reliance

If there is an instance of Class B in Class A, it is said that Class A has a dependency on class B. For example, the following class Human uses a Father object, and we say that class Human has a dependency on the class Father.

public class Human {
...
Father Father;
...
Public Human () {
Father = new father ();
}
}

Looking closely at this code, we will find that there are some problems:
(1). If you want to change the Father generation mode now, if you need to initialize the father with new father (String name), you need to modify the Human code;
(2). If it is difficult to test the effect of different Father objects on Human, the initialization of Father is written dead in the Human constructor;
(3). If the new Father () process is very slow, it is also difficult for us to Mock off the process with an already initialized Father object.

2. Dependency Injection

The above dependency on direct initialization in the constructor is a hard init method, with the disadvantage that two classes are not independent enough to be tested easily. We also have another Init method, as follows:

public class Human {
...
Father Father;
...
Public Human (Father Father) {
This.father = Father;
}
}

In the above code, we pass in the Father object as a parameter to the constructor. The Father object has been initialized externally before calling Human's constructor method. Like this kind of non-self-initiated dependency, and through the external to pass through the dependency, we are called dependency injection.
Now we find that the two problems in 1 above are well solved, and simply say that dependency injection has two benefits:
(1). Decoupling, decoupling the dependencies.
(2). Because it is decoupled, it is convenient to do unit tests, especially Mock tests.

Software development--Dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.