SOLID: Foundation for Software

Manish Dixit
2 min readSep 30, 2023

--

Source : github

SOLID is an acronym for a set of five design principles that help us create clean, maintainable, and scalable software. Each letter in SOLID stands for one of these principles:

  • Single Responsibility Principle (SRP)
  • Open-Closed Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP).

Let’s go through each principle with examples.

1. Single Responsibility Principle (SRP):

A class should have only one reason to change.

Lets say in our article management system, we have a `Article` class. Instead of putting both the responsibility of managing the article’s content and formatting, it’s better to split it into two classes:

  • Article class responsible for content.
  • Formatter class responsible for formatting.

This way, if you need to change the formatting logic, it won’t affect the article’s content management, and vice versa.

2. Open-Closed Principle (OCP):

Software entities (classes, modules, functions) should be open for extension but closed for modification.

Suppose we have a ReportGenerator class that generates various types of reports. Instead of modifying the ReportGenerator class every time we want to add a new report type, we can create new report-specific classes that inherit from an abstract Report class:

  • PDFReport extends Report
  • CSVReport extends Report

This way, we can add new report types without changing the ReportGenerator class.

3. Liskov Substitution Principle (LSP):

Subtypes must be substitutable for their base types without altering the correctness of the program.

Lets say If we have a base class Shape with methods like calculateArea(), any subclass like Circle or Rectangle should be able to substitute Shape without breaking the program’s logic. They should implement the same methods and follow the same contracts.

4. Interface Segregation Principle (ISP):

Clients should not be forced to depend on interfaces they do not use.

For example, If we have an ArticleEditor class that can edit articles and save them to a database, it should not implement a large interface with methods unrelated to article editing. Instead, we should create smaller, focused interfaces like ArticleEditable and DatabaseStorable and have the class implement only the interfaces it needs.

5. Dependency Inversion Principle (DIP):

High-level modules should not depend on low-level modules. Both should depend on abstractions.

For Example, If our article management system uses a DatabaseConnection class, the high-level ArticleService should not directly depend on the low-level DatabaseConnection. Instead, it should depend on an abstraction, such as an IDatabaseConnection interface. This allows us to change the database connection without affecting the high-level logic.

Incorporating these SOLID principles into your codebase can improve maintainability, flexibility, and the ease of making changes without introducing bugs. It’s essential for developer to grasp these principles because they form the foundation of writing clean and scalable code.

--

--

Manish Dixit
Manish Dixit

No responses yet