A set of principles to help keep large object-oriented codebases understandable, maintainable, extensible and testable.
Created by Robert C Martin in 2000 paper Design Principles and Design Patterns.
Single Responsibility Principle (SRP)
- A class should have one and only one responsibility.
- All its methods and data should support that responsibility.
- Makes classes more focused, easier to test and maintain.
Open/Closed Principle (OCP)
- Software entities should be open for extension but closed for modification
- Makes it easier to make changes to the system in the future
- Avoids having to modify existing code
- Use abstractions like inheritance or interfaces.
Liskov Substitution Principle (LSP)
- Objects of a superclass should be replaceable with objects of a subclass.
- Ensures that the program remains correct even after substitution.
- Supports substituting objects of a derived class for objects of the base class.
- Derived classes can extend the base class without changing behaviour.
Interface Segregation Principle (ISP)
- Clients should not be forced to depend on interfaces they don’t use.
- Avoids implementation of unused methods, resulting in better software design.
- Promotes separation of concerns.
- Encourage lots of smaller interfaces instead of few large interfaces.
- Preferences composition over inheritance.
Dependency Inversion Principle (DIP)
- High-level modules should not depend on low-level modules
- Both should depend on abstractions
- Promotes separation of concerns and makes the system more flexible and scalable
- By following these principles, you can write software that is maintainable, scalable, and robust. SOLID provides a foundation for writing high-quality software that is easy to modify, test, and extend.
- Examples: Inversion of Control (IOC), Dependency Injection.