Home Java javaTutorial What is the difference between Inversion of Control and Dependency Injection?

What is the difference between Inversion of Control and Dependency Injection?

Apr 24, 2019 pm 02:44 PM
dependency injection Inversion of control

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 .

What is the difference between Inversion of Control and Dependency Injection?

【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:

What is the difference between Inversion of Control and Dependency Injection?

When there is an IOC/DI container, class A no longer actively creates C, as shown in the figure:

What is the difference between Inversion of Control and Dependency Injection?

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:

What is the difference between Inversion of Control and Dependency Injection?

                                   

(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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A step-by-step guide to understanding dependency injection in Angular A step-by-step guide to understanding dependency injection in Angular Dec 02, 2022 pm 09:14 PM

This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

How to use dependency injection (Dependency Injection) in the Phalcon framework How to use dependency injection (Dependency Injection) in the Phalcon framework Jul 30, 2023 pm 09:03 PM

Introduction to the method of using dependency injection (DependencyInjection) in the Phalcon framework: In modern software development, dependency injection (DependencyInjection) is a common design pattern aimed at improving the maintainability and testability of the code. As a fast and low-cost PHP framework, the Phalcon framework also supports the use of dependency injection to manage and organize application dependencies. This article will introduce you how to use the Phalcon framework

Dependency injection pattern in Golang function parameter passing Dependency injection pattern in Golang function parameter passing Apr 14, 2024 am 10:15 AM

In Go, the dependency injection (DI) mode is implemented through function parameter passing, including value passing and pointer passing. In the DI pattern, dependencies are usually passed as pointers to improve decoupling, reduce lock contention, and support testability. By using pointers, the function is decoupled from the concrete implementation because it only depends on the interface type. Pointer passing also reduces the overhead of passing large objects, thereby reducing lock contention. Additionally, DI pattern makes it easy to write unit tests for functions using DI pattern since dependencies can be easily mocked.

Dependency injection using JUnit unit testing framework Dependency injection using JUnit unit testing framework Apr 19, 2024 am 08:42 AM

For testing dependency injection using JUnit, the summary is as follows: Use mock objects to create dependencies: @Mock annotation can create mock objects of dependencies. Set test data: The @Before method runs before each test method and is used to set test data. Configure mock behavior: The Mockito.when() method configures the expected behavior of the mock object. Verify results: assertEquals() asserts to check whether the actual results match the expected values. Practical application: You can use a dependency injection framework (such as Spring Framework) to inject dependencies, and verify the correctness of the injection and the normal operation of the code through JUnit unit testing.

In-depth understanding of inversion of control in Go language In-depth understanding of inversion of control in Go language Apr 08, 2024 am 08:51 AM

Inversion of Control (IoC) is a software design pattern that separates object dependencies into hard-coded couplings. In Go, IoC can be achieved through interfaces and dependency injection (DI): Interface: Defines the set of methods that types following the interface must implement. Dependency injection: External configuration or code generation sets object dependencies. Tips include: Constructor injection: Specifying dependencies in the constructor. Field injection: Use reflection or code generation to inject dependencies into fields. Interface injection: Passing interface types as parameters to functions or methods.

Dependency injection and service container for PHP functions Dependency injection and service container for PHP functions Apr 27, 2024 pm 01:39 PM

Answer: Dependency injection and service containers in PHP help to flexibly manage dependencies and improve code testability. Dependency injection: Pass dependencies through the container to avoid direct creation within the function, improving flexibility. Service container: stores dependency instances for easy access in the program, further enhancing loose coupling. Practical case: The sample application demonstrates the practical application of dependency injection and service containers, injecting dependencies into the controller, reflecting the advantages of loose coupling.

How to use dependency injection for unit testing in Golang? How to use dependency injection for unit testing in Golang? Jun 02, 2024 pm 08:41 PM

Using dependency injection (DI) in Golang unit testing can isolate the code to be tested, simplifying test setup and maintenance. Popular DI libraries include wire and go-inject, which can generate dependency stubs or mocks for testing. The steps of DI testing include setting dependencies, setting up test cases and asserting results. An example of using DI to test an HTTP request handling function shows how easy it is to isolate and test code without actual dependencies or communication.

Go Language: Dependency Injection Guide Go Language: Dependency Injection Guide Apr 07, 2024 pm 12:33 PM

Answer: In Go language, dependency injection can be implemented through interfaces and structures. Define an interface that describes the behavior of dependencies. Create a structure that implements this interface. Inject dependencies through interfaces as parameters in functions. Allows easy replacement of dependencies in testing or different scenarios.

See all articles