# SOLID Five OOP design principles that make software more understandable, flexible, and maintainable. ## S — Single Responsibility Principle (SRP) A class should have only one reason to change. - **Bad**: A `User` class handling authentication, email sending, and database persistence - **Good**: Separate `UserAuthenticator`, `EmailService`, and `UserRepository` classes ## O — Open/Closed Principle (OCP) Open for extension, closed for modification. - **Bad**: Adding a new shape requires modifying `drawShape()` with another `if/else` - **Good**: Each shape implements a `draw()` method; new shapes just implement the interface ## L — Liskov Substitution Principle (LSP) Subtypes must be substitutable for their base types without breaking correctness. - **Bad**: `Square` extending `Rectangle` where `setWidth()` also changes height - **Good**: Both implement a `Shape` interface with `area()` ## I — Interface Segregation Principle (ISP) Clients should not depend on interfaces they don't use. - **Bad**: A `Worker` interface with `work()`, `eat()`, `sleep()` — a `Robot` doesn't eat or sleep - **Good**: Separate `Workable`, `Eatable`, `Sleepable` interfaces ## D — Dependency Inversion Principle (DIP) High-level modules should depend on abstractions, not low-level modules. - **Bad**: `NotificationService` directly instantiates `SmtpEmailSender` - **Good**: `NotificationService` depends on a `MessageSender` interface; implementation injected at runtime --- See also: [[OOP]], [[Design Patterns]], [[Composition vs Inheritance]]