Home >Java >javaTutorial >What is the difference between Inversion of Control and Dependency Injection?
The differences between inversion of control and dependency injection are: dependency injection is described from the perspective of the application, while inversion of control is described from the perspective of the container; inversion of control focuses on principles, while dependency injection focuses on implementation .
【Recommended course: Java Tutorial】
The difference between inversion of control and dependency injection
IOC Inversion of control Inversion of control
DI Dependency Injection Dependency injection
Requirements To understand these two concepts, we must first clarify the following questions:
Who are the participants?
Dependence: Who depends on whom? Why do we need dependencies?
Injection: Who injects into whom? What exactly is injected?
Inversion of Control: Who controls whom? Control what? Why is it called a reversal (if there is a reversal, there should be a forward reversal)?
Are dependency injection and inversion of control the same concept?
Let’s briefly answer the above questions. Once these questions are understood, IOC/DI will also be understood.
(1) Who are the participants:
Generally there are three parties, one is a certain object; one is the IOC/DI container; the other is An external resource for an object.
An object refers to any ordinary Java object
The IOC/DI container simply refers to a framework program used to implement the IOC/DI function
The external resources of the object refer to what the object needs, but they are obtained from outside the object. They are collectively called resources, such as: other objects needed by the object, or file resources needed by the object, etc.
(2) Who depends on whom:
Of course, a certain object depends on the IOC/DI container
(3) Why dependencies are needed:
The object requires an IOC/DI container to provide the external resources required by the object
(4) Who injects into whom:
It is obvious that the IOC/DI container injects an object
(5)What exactly is injected:
That’s it Inject external resources required for an object
(6) Who controls whom:
Of course it is the IOC/DI container that controls the object
(7) What to control:
Mainly controls the creation of object instances
(8) Why is it called reverse Turn:
Reversal is relative to the positive direction, so what is considered positive? Think about the application under normal circumstances. If you want to use C inside A, what would you do? Of course, the object of C is created directly, that is, the required external resource C is actively obtained in class A. This situation is called forward. So what is reverse? That is, class A no longer actively obtains C, but passively waits for the IOC/DI container to obtain an instance of C, and then injects it into class A in reverse.
Use an illustration to illustrate. Let’s first look at the schematic diagram of conventional Class A using Class C when there is no IOC/DI, as shown in the figure:
When there is an IOC/DI container, class A no longer actively creates C, as shown in the figure:
but passively Wait, wait for the IOC/DI container to obtain an instance of C, and then reversely inject it into class A, as shown in the figure:
(9) Are dependency injection and inversion of control the same concept?
According to the above description, you should be able to see that dependency injection and inversion of control are different descriptions of the same thing. In a certain aspect, they describe it from different angles. Dependency injection is described from the perspective of the application. The complete description is: the application relies on the container to create and inject the external resources it needs; while inversion of control is described from the perspective of the container. The complete description is: the container controls the application. The container reversely injects the external resources required by the application into the application.
Summary:In fact, the biggest change that IOC/DI brings to programming is not from the code, but from the perspective of thinking, a "master-slave transposition" has occurred The change. The application was originally the boss and took the initiative to obtain any resources. However, in the IOC/DI thinking, the application becomes passive, passively waiting for the IOC/DI container to create and inject the resources it needs. Such a small change is actually a big progress in programming thinking, which effectively separates the object and the external resources it requires, making them loosely coupled, conducive to functional reuse, and more importantly, making the entire system of the program The structure becomes very flexible
The above is the detailed content of What is the difference between Inversion of Control and Dependency Injection?. For more information, please follow other related articles on the PHP Chinese website!