Definition “Inversion of control” What does inversion of control mean?
Inversion of Control is a design principle or development paradigm in which the control flow of an application is outsourced to another location. A common example is that a framework takes over the control of certain parts of the program.
The programming principle Inversion of Control (IoC) reverses the conventional control flow of a program. Without IoC, the control flow would be such that the program code written by the developer calls up resources from libraries to use them. The objects are statically connected to each other.
If the control flow is reversed by inversion of Control, the code from the framework dynamically accesses the developer’s custom code instead. Framework and custom code are now only loosely coupled.
Benefits of Inversion of Control
Many advantages of the control reversal are related to the fact that, as already mentioned, dependencies are only loosely coupled to each other. Or more generally, the implementation and the execution of a task are decoupled from each other by inversion of Control.
This means that the reusable parts of the code that are useful more often and in different places of the program can be developed independently of the places where a specific problem is solved. Although they may only lead to the desired result together, they do not have to be developed or adapted at the same time. Further thought, this has the advantage that it becomes easier to switch between different implementations (e.g. from a framework).
Programs are also easier to design modularly, especially when it comes to complex code. In other words, it is easier to make modules more independent of the rest of the code. For example, more focus can be placed on modules that are compact and tailored to a specific task. There are also fewer unforeseen effects on the entire project when a single module is replaced. Likewise, testing the software can become easier if the components are kept better isolated from each other.
Disadvantages of Inversion of Control
Reversing the control flow is in many cases a sensible option to keep a complex system maintainable and clear. Nevertheless, one should carefully check whether it is really the right approach. Because IoC also means a higher effort in the design, implementation and maintenance of software.
There are scenarios in which this additional work does not pay off or is currently in conflict with the higher quality criteria of the project. If, for example, the top priority is to quickly see the first results or to be able to prove the suitability of a concept, it may be better to switch to Inversion of Control at a later date.
Examples of design patterns based on Inversion of Control:
Dependency injection: An object does not manage its dependencies itself, but gets them assigned by a separate piece of code. The dependencies are stored on a separate component and can thus be managed more clearly.
Service Locator Pattern: The Service Locator, a central register, serves as an intermediary and, upon request, passes on all the information that is needed for the requested task. In this way, different parts of a library can exist encapsulated from each other and can still be connected via the service locator.
Strategy Patterns: At runtime, one of several algorithms is selected via an intermediary class depending on the context.
As of 30.10.2020
It is a matter of course for us that we handle your personal data responsibly. If we collect personal data from you, we process it in compliance with the applicable data protection regulations. Detailed information can be found in our privacy policy.
Consent to the use of data for advertising purposes
I agree that Vogel IT-Medien GmbH, Max-Josef-Metzger-Straße 21, 86157 Augsburg, including all companies affiliated with it within the meaning of §§ 15 et seq. AktG (hereinafter: Vogel Communications Group) may use my e-mail address for sending editorial newsletters. Lists of the associated companies can be found here.
The newsletter content covers products and services of all the aforementioned companies, including, for example, trade journals and specialist books, events and trade fairs as well as event-related products and services, print and digital media offers and services such as further (editorial) newsletters, competitions, lead campaigns, market research in the online and offline area, subject-specific web portals and e-learning offers. If my personal telephone number has also been collected, it may be used for the submission of offers of the aforementioned products and services of the aforementioned companies and market research.
If I call up protected content on the Internet on portals of the Vogel Communications Group, including its affiliated companies within the meaning of §§ 15 et seq. AktG, I must register with further data for access to this content. In return for this royalty-free access to editorial content, my data may be used for the purposes mentioned here within the meaning of this consent.
Right of withdrawal
I am aware that I can revoke this consent at any time for the future. My revocation does not affect the legality of the processing carried out on the basis of my consent until the revocation. To explain my revocation, I can, as a possibility, do this under https://support.vogel.de use the available contact form. If I no longer wish to receive individual newsletters I have subscribed to, I can also click on the unsubscribe link included at the end of a newsletter. Further information on my right of revocation and its exercise as well as on the consequences of my revocation can be found in the privacy policy, section Editorial newsletters.