Sobes.tech
Junior — Middle

Can you name the main approaches to implementing the Inversion of Control (IoC) principle?

sobes.tech AI

Answer from AI

Main approaches to implementing the Inversion of Control (IoC) principle in Java:

  1. Dependency Injection (DI) — dependencies are passed to an object from outside, usually through constructors, setters, or interfaces. This allows separating object creation from their usage.
public class Service {
    private Repository repository;

    // Injection via constructor
    public Service(Repository repository) {
        this.repository = repository;
    }
}
  1. IoC Containers (e.g., Spring Framework) — frameworks that manage the lifecycle of objects and their dependencies automatically.

  2. Factory methods and abstract factories — objects are created through factories, allowing to change specific implementations without modifying the client code.

All these approaches help reduce component coupling and increase testability and flexibility of the application.