Definition: A class should have only one reason to change, meaning it should do only one job.
Why:
-
Reduces complexity
-
Improves testability and reusability
Example (Java/Spring Boot):
Bad Practice:
Good Practice:
Definition: Software entities (classes, modules, functions) should be open for extension but closed for modification.
Why:
-
Add new features without changing existing code
-
Prevents breaking existing functionality
Example: Instead of modifying an existing payment processor, create new implementations:
Definition: Subclasses should be replaceable with their base class without breaking the application.
Why:
-
Ensures correct polymorphism usage
-
Avoids unexpected behaviors
Example:
Bad Practice:
Good Practice:
Definition: No client should be forced to depend on methods it does not use.
Why:
-
Avoids "fat" interfaces
-
Encourages specific, smaller interfaces
Example:
Bad Practice:
Good Practice:
Definition: High-level modules should not depend on low-level modules; both should depend on abstractions.
Why:
-
Improves flexibility
-
Makes code testable
Example (Spring Boot DI):
💡 In short:
-
S – One class = One job
-
O – Add new behavior without changing old code
-
L – Subtypes must behave like their base types
-
I – Keep interfaces small and specific
-
D – Depend on abstractions, not implementations