Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What does control reversal mean?

Shulou Source: shulou.com Published: 2022-06-03 02:56:16 10月19日 Update

This article is to share with you what is the meaning of control reversal, Xiaobian thinks it is quite practical, so share it with you to learn, I hope you can gain something after reading this article, don't say much, follow Xiaobian to have a look.

Inversion of control is a design principle in object-oriented programming that can be used to reduce coupling between computer code and to allow objects to be passed references to objects they depend on by an external entity that regulates all objects in the system when they are created.

Inversion of Control (IoC) is a design principle used in object-oriented programming to reduce coupling between computer code. The most common method is called dependency injection (DI), and another method is called dependency lookup. By inversion of control, an object is passed references to objects it depends on when it is created by an external entity that regulates all objects in the system. It can also be said that dependencies are injected into objects.

Control inversion implementation strategy

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

Dependency lookup: containers provide callback interfaces and context conditions to components. EJB and Apache Avalon both use this approach. As a result, the component must use the API provided by the container to find resources and collaboration objects, and the only inversion of control is on those callback methods (i.e., type 1 above): the container will call these callback methods, allowing the application code to obtain the relevant resources.

Dependency injection: Components do not do location queries, but provide normal Java methods for containers to determine dependencies. The container is solely responsible for assembly of components, passing objects that match dependencies to the desired objects through JavaBean properties or constructors. Injecting dependencies through JavaBean attributes is called Setter Injection; passing dependencies as constructor parameters is called Constructor Injection

Control inversion realization method

Implementing the Data Access Layer

The data access layer has two goals. The first is to abstract the database engine from the application so that you can change the database at any time-say, from Microsoft SQL to Oracle. However, this is rarely done in practice, and there is not enough reason or ability to refactor existing applications by using the implementation data access layer.

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

Module and interface reconfiguration

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

Add unit tests anytime

Encapsulating functionality into an entire object makes automated testing difficult or impossible. Refactoring in such a way that modules and interfaces are isolated from specific objects allows more advanced unit testing to be performed. It is tempting to continue refactoring modules with the idea of adding tests later, but it is wrong.

Use service locator instead of construct injection

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

The above is what control inversion means. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Object control data system method refactoring container way application code principle database component test interface time module goal part meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Redmi Shulou Information MySQL