Dorokhov.codes
Inversion of Control (IoC)
Inversion of Control (IoC) is a software design principle that refers to a pattern where the control flow of a program is inverted or “inverted” from the traditional procedural flow. In the traditional procedural flow, a program controls the flow of execution by directly calling and coordinating various functions or objects.
IoC typically involves a framework or container that manages the flow of control and dependencies in an application. Instead of a developer manually creating and managing object instances and their dependencies, the IoC container takes responsibility for creating and wiring objects together.
There are two common forms of IoC:
- Dependency Injection (DI): This is the most common form of IoC, where the dependencies of a class or object are “injected” from an external source rather than being created within the class itself. The external source is typically an IoC container, which manages the creation and wiring of objects. DI can be achieved through constructor injection, setter injection, or interface injection.
- Dependency Lookup: In this form of IoC, instead of objects being injected, a class or object looks up its dependencies from an external source or container when needed. The class explicitly asks for its dependencies from the container, usually through a lookup method or service locator.