Member-only story
SOLID Principles
6 min readNov 17, 2023
The SOLID principles are a set of five design principles for writing maintainable and scalable software. These principles were introduced by Robert C. Martin and are widely used in object-oriented programming. The SOLID acronym stands for:
- Single Responsibility
- Open/Closed
- Liskov Substitution
- Interface Segregation
- Dependency Inversion
Single Responsibility Principle (SRP):
- Definition: A class should have only one reason to change.
- Explanation: The Single Responsibility Principle (SRP) suggests that a class should have only one responsibility or job. In other words, a class should encapsulate only one aspect of the software’s functionality. This principle aims to keep classes focused, maintainable, and easier to understand. If a class has more than one reason to change, it becomes more error-prone and harder to maintain.
- Example Explanation: The first set of classes violated SRP by combining report generation and database interaction in a single class (
Report
). The corrected example adheres to SRP by separating these responsibilities into two classes (Report
andDatabaseSaver
), each with a single responsibility.
// Violation of SRP
class Report {
public void…