I’m new to Spring and currently learning it through some paid courses. I came across the concept of loose coupling, and I understand that Spring promotes defining behavior in interfaces. However, I am wondering why it’s necessary to define behavior in an interface instead of directly extending the implementation in a class?
For example, instead of:
public class Car extends PetrolEngine {
// implementation
}
Why is it recommended to define an interface like this:
public interface Engine {
void start();
}
public class PetrolEngine implements Engine {
public void start() {
// implementation
}
}
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}
What are the advantages of using interfaces in this context, and how does this help with loose coupling in Spring?
1
Simple answer:
Spring DI via interface ensures that your code is:
Unit Testable
Reusable
Maintainable
Loosely coupled
andhighly cohesive
For example:
public interface Engine {
void start();
}
public class PetrolEngine implements Engine {
public void start() {
// implementation
}
}
public class DieselEngine implements Engine {
public void start() {
// implementation
}
}
@Component
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}
@Configuration
public class CarConfig {
@Bean
public Engine engine() {
return new PetrolEngine(); // or new DieselEngine(); based on requirement.
}
}
When Spring will try to inject the bean into the engine instance variable present in the Car
component, it will check in the IOC container and find that it has the bean with the name engine
(internally referencing to PetrolEngine
or DieselEngine
object)