Home > Common Problem > body text

What does inversion of control mean?

Guanhui
Release: 2020-06-28 17:40:22
Original
8214 people have browsed it

Inversion of control is a design principle in object-oriented programming. It can be used to reduce the coupling between computer codes. It can also use inversion of control to allow objects to be controlled by a controller when they are created. External entities of all objects in the system pass references to the objects on which they depend.

What does inversion of control mean?

Inversion of Control (IoC) is a design principle in object-oriented programming that can be used to reduce the number of errors between computer codes. degree of coupling. The most common method is called Dependency Injection (DI), and the other method is called Dependency Lookup. Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it. It can also be said that dependencies are injected into the object.

Inversion of Control Implementation Strategy

IoC is a big concept that can be implemented in different ways. There are two main forms:

Dependency lookup: The container provides callback interfaces and contextual conditions to components. Both EJB and Apache Avalon use this approach. In this way, the component must use the API provided by the container to find resources and collaborative objects. The only inversion of control is only reflected in those callback methods (that is, type 1 mentioned above): the container will call these callback methods, This allows application code to obtain relevant resources.

Dependency injection: The component does not perform positioning queries and only provides ordinary Java methods for the container to determine dependencies. The container is solely responsible for the assembly of components. It will pass objects that meet dependencies to the required objects through JavaBean properties or constructors. The practice of injecting dependencies through JavaBean properties is called Setter Injection; the practice of passing dependencies as constructor parameters is called Constructor Injection

Control the reaction Transfer to implementation method

Implement the data access layer

The data access layer has two goals. The first is to abstract the database engine from the application so that the database can be changed at any time—for example, from Microsoft SQL to Oracle. However, this is rarely done in practice, and there is not enough reason and ability to reconstruct existing applications by using the data access layer.

The second goal is to abstract the data model from the database implementation. This allows the database or code to be changed as needed while affecting only a small part of the main application - the data access layer. This goal is worth the necessary refactoring to achieve it in existing systems.

Module and Interface Refactoring

One of the core ideas behind dependency injection is the single responsibility principle. This principle states that every object should have a specific purpose, and that different parts of the application that need to exploit that purpose should use appropriate objects. This means that these objects can be reused anywhere in the system. But this is not the case in many existing systems.

Add unit tests at any time

Encapsulating functions into the entire object will make automatic testing difficult or impossible. Refactoring in this way allows for more advanced unit testing by isolating modules and interfaces from specific objects. It's tempting to continue refactoring a module with the idea of ​​adding tests later, but this is a mistake.

Use service locators instead of constructor injection

There is more than one way to achieve inversion of control. The most common approach is to use constructor injection, which requires all software dependencies to be provided when the object is first created. However, constructor injection assumes that the entire system uses this pattern, which means that the entire system must be refactored simultaneously. It's difficult, risky, and time-consuming.

Recommended tutorial: "PHP"

The above is the detailed content of What does inversion of control mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!