Junior — Middle
Kas saaksite selgitada kontrolli pööramise kontseptsiooni ja selle rolli tarkvarasüsteemide arendamisel?
sobes.tech AI
Vastus AI-lt
Inversion of Control (IoC) is a design principle where the control flow of a program is transferred to an external container or framework, rather than being implemented directly in the application code.
Instead of an object creating and managing its dependencies itself, these dependencies are injected from outside (for example, via constructor or setter). This increases modularity, simplifies testing and maintenance.
In Java, this is often implemented through Dependency Injection, where the container (e.g., Spring) creates objects and manages their lifecycle.
Example of dependency injection:
public class Service {
private Repository repository;
// Dependency injection via constructor
public Service(Repository repository) {
this.repository = repository;
}
public void doWork() {
repository.save();
}
}
Here, Service does not create the Repository itself but receives it from outside, which conforms to the IoC principle.