Last Updated: November 21, 2025
SOLID Principles
Object-oriented design principles
SOLID Overview
| Item | Description |
|---|---|
S
|
Single Responsibility Principle |
O
|
Open-Closed Principle |
L
|
Liskov Substitution Principle |
I
|
Interface Segregation Principle |
D
|
Dependency Inversion Principle |
Single Responsibility
- A class should have only one reason to change
- Each class should do one thing and do it well
- Separates concerns for better maintainability
- Example: User class shouldn't handle database AND validation
Open-Closed
- Open for extension, closed for modification
- Add new functionality without changing existing code
- Use inheritance, interfaces, composition
- Example: Plugin architecture
Liskov Substitution
- Subclasses should be substitutable for base classes
- Don't break parent class contracts
- Derived classes must honor base class behavior
- Example: Square shouldn't inherit Rectangle if it violates width/height independence
Interface Segregation
- Clients shouldn't depend on interfaces they don't use
- Many specific interfaces better than one general
- Avoid 'fat' interfaces
- Example: Separate IPrintable, IScannable instead of IMultiFunction
Dependency Inversion
- Depend on abstractions, not concretions
- High-level modules shouldn't depend on low-level modules
- Both should depend on abstractions
- Example: Use interfaces for dependencies, inject implementations
💡 Pro Tips
Quick Reference
Write maintainable object-oriented code