官术网_书友最值得收藏!

Introducing DI

DI is a software development technique where we can create objects that depend on other objects. DI helps the interaction between classes, but at the same time keeps the classes independent. 

There are three types of classes in DI:

  • A Service is a class that can be used (dependency).
  • The Client is a class that uses dependency.
  • The Injector passes the dependency (Service) to the dependent class (Client).

The three types of classes in DI are shown in the following diagram:

DI makes classes loosely coupled. This means that the creation of client dependencies is separated from the client's behavior, which makes unit testing easier.

Let's take a look at a simplified example of DI using Java code. In the following code, we don't have DI, because the client Car class is creating an object of the service class:

public class Car {
private Owner owner;

public Car() {
owner = new Owner();
}
}

In the following code, the service object is not directly created in the client class. It is passed as a parameter in the class constructor:

public class Car {
private Owner owner;

public Car(Owner owner) {
this.owner = owner;
}
}

The service class can also be some abstract class, then we can use any implementation of that in our client class and use mocks when testing.

There are different types of DI, for example, the following two types:

  • Constructor injection: Dependencies are passed to a client class constructor. An example of the constructor injection was already shown in the preceding Car example code. 
  • Setter injection: Dependencies are provided through setters. The following example code shows an example of the setter injection:
public class Car {
private Owner owner;

public void setOwner(Owner owner) {
this.owner = owner;
}
}
主站蜘蛛池模板: 化隆| 长岛县| 乌苏市| 鄂伦春自治旗| 嘉禾县| 蒙城县| 广平县| 彭水| 集安市| 中超| 恩施市| 武乡县| 西和县| 江达县| 太康县| 图片| 彭州市| 石台县| 凯里市| 百色市| 塔河县| 甘德县| 高平市| 金寨县| 六枝特区| 屯留县| 锦州市| 中方县| 阿合奇县| 二连浩特市| 电白县| 泸定县| 自贡市| 冷水江市| 南开区| 高尔夫| 安福县| 邹城市| 全南县| 阜阳市| 阿坝|