Coupling & Cohesion
Coupling & Cohesion — -> The OOPs concept that finally made Spring Boot make sense for me.
Coupling and cohesion are not just Java concepts.
They are core design principles, and Spring Boot is built around enforcing them in real projects.
👉Coupling -> external dependency between classes
Coupling refers to the degree to which one class depends on another class.
— High dependency → tightly coupled
— Low dependency → loosely coupled
When code is loosely coupled:
— Changes are easier
— Testing becomes simpler
— Future enhancements don’t break the system easily
When code is tightly coupled:
— A small change causes ripple effects
— Testing becomes painful
— Maintenance cost increases heavily
# consider classes are in same package
class A{
static int i=B.j;
}
class B{
static int j=C.k;
}
class C{
static int k=D.m1();
}
class D{
public static void m1(){
return 10;
}
}
# Each classes are tightly depending on each other.👉Cohesion -> internal responsibility of a class
Cohesion is about how well-focused a class or module is on a single responsibility.
High cohesion means:
— One class → one clear purpose
— Logic is grouped meaningfully
— Code is readable without extra explanation
For example:
— EmailService → only email-related logic
— OrderService → only order-related logic
— InvoiceService → only invoice-related logic
If we mix email, order, invoice, and utility logic in one class:
— Understanding the code becomes difficult
— Debugging becomes slower
— Testing individual features becomes messy
That’s a sign of low cohesion.
# Low cohesion example
class Utils {
validateUser();
sendEmail();
generatePDF();
}
#High cohesion example
class EmailService {
sendEmail();
validateEmail();
}
# High cohesion is always recomended.— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
👉How does Spring Boot actively reduce coupling?
This is where Spring Boot becomes powerful.
Spring Boot encourages dependency injection, which means:
— Classes depend on abstractions, not concrete implementations
— Objects are wired by the framework, not manually created
— Changing implementations does not affect the calling code
Because of this:
— Services don’t know how dependencies are created
— Code becomes flexible and testable
— Mocking and unit testing become natural
👉How does Spring Boot promote high cohesion?
Spring Boot naturally pushes us toward high cohesion through:
— Layered architecture (Controller, Service, Repository)
— Clear separation of responsibilities
— Package-based organization
Annotations like @Controller, @Service, and @Repository are not just annotations —
they are design signals that tell us what responsibility a class should have.
Get Raushan Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
👇
Earlier, Spring Boot felt like:
— Annotations
— Configuration
— Framework magic
After understanding coupling and cohesion properly, Spring Boot started feeling like:
— A design enforcer
— A framework that guides good architecture
— A tool that makes OOPS principles practical at scale







