How Spring IOC Reduces Coupling: From Direct Instantiation to Interfaces, Factory Method, and Reflection
This article explains the concept of coupling, demonstrates how traditional object creation leads to high coupling, and shows how using interfaces, the factory pattern, and reflection—culminating in Spring IOC—can dramatically lower coupling, improve reuse, and simplify maintenance in Java backend development.
Coupling refers to the degree of dependency between different parts of a system; high coupling makes maintenance and scalability difficult, while low coupling promotes flexibility.
1. Traditional Object Creation
Creating objects directly with new leads to repetitive code. For example:
Food food = new Food();
food.eat(); Play play = new Play();
play.with(); Friend friend = new Friend();
friend.get();If the same pattern appears thousands of times, any change requires editing many lines of code.
2. Interface Programming
Define a common interface for activities:
public interface Activity {
void act();
}Implement the interface for each concrete class:
public class Food implements Activity {
@Override
public void act() {
System.out.println("吃东西");
}
} public class Play implements Activity {
@Override
public void act() {
System.out.println("去玩耍");
}
} public class Friend implements Activity {
@Override
public void act() {
System.out.println("交朋友");
}
}This reduces code duplication but still requires the client to instantiate each class.
3. Factory Method
A factory class centralizes object creation:
class ActivityFactory {
public static Activity getInstance(String type) {
Activity result = null;
if ("food".equals(type)) { result = new Food(); }
if ("play".equals(type)) { result = new Play(); }
if ("friend".equals(type)) { result = new Friend(); }
return result;
}
}
Activity activity = ActivityFactory.getInstance("food");
activity.act();The factory decouples client code from concrete classes, improving cohesion and maintainability, though the client still needs to specify the type.
4. Reflection
Using reflection, the concrete class can be determined at runtime from a configuration file:
Activity activity = ActivityFactory.getInstance(readConfig());
activity.act();
// e.g., the config requests "Friend"This eliminates the need to modify source code when changing behavior.
5. Spring IOC
Spring IOC combines the factory pattern with reflection to automatically create and manage objects, handling their lifecycle and dependencies, thereby achieving low coupling.
Conclusion
Dependency injection means objects are provided by an external container rather than being created manually, resulting in:
Reduced coupling : Objects are loosely connected, enhancing flexibility and extensibility.
Improved code reuse : Common creation logic is centralized.
Simplified code : Clients no longer need to know creation details.
Easier testing : Dependencies can be swapped or mocked easily.
Support for AOP : Enables cross‑cutting concerns to be handled uniformly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.