Junior — Middle
What mechanisms or approaches have you used to implement dependency inversion in your last project?
sobes.tech AI
Answer from AI
To implement dependency inversion in my last project, I used the Dependency Injection pattern. Specifically, I applied constructor injection, where dependencies were passed through the class constructor. This allowed for easy replacement of dependency implementations for testing and improved code modularity.
Example in Java:
public class Service {
private final Repository repository;
public Service(Repository repository) {
this.repository = repository;
}
public void perform() {
repository.save();
}
}
Here, Service does not create Repository itself but receives it from outside, which is the essence of dependency inversion.