*** ### What is Inversion of Control (IoC)? **Process in which an object defines its dependencies without creating them.** This object delegates the job of constructing such dependencies to an IoC container. ### When is recommended to use Inversion of Control? Imagine an application with dozens or even hundreds of classes. Sometimes we want to share a single instance of a class across the whole application, other times we need a separate object for each use case, and so on. Managing such a number of objects is nothing short of a nightmare. **This is where inversion of control comes to the rescue.** ### What are the advantages of this architecture? In contrast with traditional programming, in which our custom code makes calls to a library, IoC enables a framework to take control of the flow of a program and make calls to our custom code. To enable this, frameworks use abstractions with additional behavior built in. **If we want to add our own behavior, we need to extend the classes of the framework or plugin our own classes.** The advantages of this architecture are: - decoupling the execution of a task from its implementation - making it easier to switch between different implementations - greater modularity of a program - greater ease in testing a program by isolating a component or mocking its dependencies, and allowing components to communicate through contracts ### How can we achieve IoC? Through various mechanisms such as: Strategy design pattern, Service Locator pattern, Factory pattern, and [[Dependency Injection | Dependency Injection (DI)]]. *** **References**: - [What is a Spring Bean?](https://www.baeldung.com/spring-bean) - [Intro to Inversion of Control and Dependency Injection with Spring](https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring)