SOLID Principles Fundamentals
SOLID is an acronym for five object-oriented design principles that lead to maintainable, extensible, and fault-resistant code.
In a Nutshell
SOLID: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, Dependency Inversion.
S — Single Responsibility (SRP)
A class should have only one reason to change.
// Bad: multiple responsibilities
class User {
saveToDatabase() { /* DB */ }
sendEmail() { /* Email */ }
validateEmail() { /* Validation */ }
}
// Good: separated concerns
class User {}
class UserRepository { save(u: User) {} }
class EmailService { send(u: User, msg: string) {} }
O — Open-Closed (OCP)
Open for extension, closed for modification.
// Bad: extend switch for each new type
class DiscountCalculator {
calculate(type: string): number {
if (type === 'premium') return 0.8;
if (type === 'vip') return 0.7;
return 1.0;
}
}
// Good: Strategy Pattern
interface DiscountStrategy { getFactor(): number; }
class PremiumDiscount implements DiscountStrategy { getFactor() { return 0.8; } }
class VipDiscount implements DiscountStrategy { getFactor() { return 0.7; } }
L — Liskov Substitution (LSP)
Subtypes must be substitutable for their base types.
// Bad: Penguin cannot fly
class Bird { fly() {} }
class Penguin extends Bird { fly() { throw new Error('Cannot fly'); } }
// Good: correct hierarchy
interface Bird { layEgg(): void; }
interface FlyingBird extends Bird { fly(): void; }
I — Interface Segregation (ISP)
No client should depend on unused methods.
// Bad: fat interface
interface Worker { work(): void; eat(): void; sleep(): void; }
// Good: segregated
interface Workable { work(): void; }
interface Eatable { eat(): void; }
D — Dependency Inversion (DIP)
Depend on abstractions, not concrete implementations.
// Bad: direct dependency
class OrderService {
private repo = new MySQLDatabase();
}
// Good: Dependency Injection
interface OrderRepository { save(order: Order): void; }
class OrderService {
constructor(private repo: OrderRepository) {}
}
SOLID at a Glance
| Principle | Meaning | Goal |
|---|---|---|
| SRP | Single Responsibility | One class, one responsibility |
| OCP | Open-Closed | Extensible without modification |
| LSP | Liskov Substitution | Subtypes replace base types |
| ISP | Interface Segregation | Small, focused interfaces |
| DIP | Dependency Inversion | Abstraction over concrete types |
Key Takeaways for Study
- SOLID is an acronym for five OOP design principles
- SRP: one reason to change per class
- OCP: extend without modifying (Strategy Pattern)
- LSP: subtypes must honor contracts
- ISP: many small, specific interfaces
- DIP: Dependency Injection, abstraction as a dependency
FAQ
1. What does SOLID stand for?
2. What does SRP state?
3. What does OCP state?
4. What does LSP state?
5. What does ISP state?
6. What does DIP state?
7. Who coined SOLID?
8. Example of LSP violation?
9. How to achieve OCP?
10. What is DI?
11. SOLID vs Clean Code?
12. What is a fat interface?
13. When should you apply SOLID?
14. Can SOLID be overdone?
15. How do SOLID and testing relate?
Continue Your SOLID Learning Path
The next article in the SOLID learning path covers Analysis and Design Techniques: UML, GRASP, SOLID and Quality Goals — how UML diagrams, GRASP patterns, and SOLID principles work together in software architecture.
References
- https://en.wikipedia.org/wiki/SOLID
- https://www.oreilly.com/library/view/clean-code/9780134661742/
- https://refactoring.guru/design-patterns/solid-principles
Recommended Books on Software Quality
If you’d like to dive deeper into SOLID, Clean Code, and software quality, check out these reads:
Software Engineering
Books about software quality, clean code, code reviews and software development processes
Clean Code: A Handbook of Agile Software Craftsmanship von Robert C. Martin
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
The Pragmatic Programmer: Your Journey to Mastery von David Thomas, Andrew Hunt
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.




