Suggest an editImprove this articleRefine the answer for “What does the Inversion of Control (IoC) principle mean?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Inversion of Control (IoC)** is a design principle in which an object does not directly control its dependencies and execution flow, but hands that control over to an external system or framework: it does not create what it needs itself, it receives it from outside. **Key point:** IoC is not a specific pattern but a general architectural principle that weakens coupling between components and increases the system's flexibility and testability.Shown above the full answer for quick recall.Answer (EN)Image**Inversion of Control (IoC)** is a design principle in which an object does not directly control its dependencies and execution flow, but hands that control over to an external system or framework. In other words, **the object does not create what it needs by itself, it receives it from outside**. The main idea is **inversion of control**: previously the program called the framework, now the framework calls the program. --- ### The core idea In the traditional imperative approach, the developer manages the creation of objects, their initialization, and their interaction. With IoC, this control is "flipped": now the **container or framework** manages the lifecycle of objects, their dependencies, and the calls between them. --- ### Example without IoC ```python class UserService: def __init__(self): self.repository = UserRepository() # dependency is created manually def get_user(self, user_id): return self.repository.find_by_id(user_id) ``` Here `UserService` creates `UserRepository` itself. This is tight coupling: replacing the repository or testing the code is difficult. --- ### Example with IoC ```python class UserService: def __init__(self, repository): self.repository = repository # dependency is passed in from outside (via the constructor) ``` Now someone **outside** decides which repository to plug in, for example a test one or a real one. Control over the dependency is inverted. --- ### Where IoC is used - In **frameworks** (Spring, .NET Core, Django): the developer writes the logic, and the framework calls it at the right moment. - In **dependency containers (IoC Containers)**: they create and wire objects together, injecting dependencies. - In **event-driven systems**: code reacts to events instead of controlling the execution flow. --- ### Ways to implement IoC 1. **Dependency Injection (DI)** - the most common approach, where dependencies are injected through a constructor, properties, or methods. 2. **Service Locator** - dependencies are requested from a centralized object (a less preferred option). 3. **Event-driven IoC** - control is handed to an event model, where code is invoked when events occur. --- ### Key benefit - Weaker coupling between components (low coupling). - Simpler testing and dependency substitution. - Increased flexibility and extensibility of the system. - The framework can take over infrastructure tasks (lifecycle, caching, logging). --- **Summary:** *IoC is not a specific pattern but a general architectural principle, in which an object does not manage its dependencies but receives them from outside. This is an inversion of control over the creation and wiring of components, which provides flexibility and testability for the system.*For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.