Coupling is the degree of dependency between classes or modules.
It defines how much one class knows about another.
Low Coupling = Good (loose dependency)
High Coupling = Bad (tight, rigid dependency)
Key Concepts
Type | Description |
---|---|
Tight Coupling | Classes are heavily dependent on each other |
Loose Coupling | Classes interact through abstractions/interfaces |
Example
class Engine {
void start() { ... }
}
class Car {
Engine engine = new Engine(); // hard-coded dependency
void startCar() {
engine.start();
}
}
interface Engine {
void start();
}
class PetrolEngine implements Engine {
public void start() { ... }
}
class Car {
Engine engine;
Car(Engine engine) {
this.engine = engine;
}
void startCar() {
engine.start(); // depends on abstraction
}
}
Comparison
Cohesion vs Coupling
Concept | Goal |
---|---|
Cohesion | Keep a class focused |
Coupling | Minimize clas interdependence |
Real-World Analogy
Tight coupling: TV remote only works with one TV model
Loose coupling: Universal remote works with any brand
Tip
Loose coupling + high cohesion = solid design.
Mention coupling when discussing interfaces, dependency injection or SOLID principles.
Parent: _Design Principles