SOLID Principles

Saurav Kumar
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 and DatabaseSaver), each with a single responsibility.
// Violation of SRP
class Report {
public void generateReport() {
// logic to generate a financial report
}

public void saveToDatabase() {
// logic to save the report to the database
}
}

// Adhering to SRP
class Report {
public void generateReport() {
// logic to generate a financial report
}
}

class DatabaseSaver {
public void saveToDatabase(Report report) {
// logic to save the report to the database
}
}

Open/Closed Principle (OCP):

  • Definition: Software entities should be open for extension but closed for modification.
  • Explanation: The Open/Closed Principle (OCP) encourages designing software entities (classes, modules, functions, etc.) so that their behavior can be extended without modifying their source code. This promotes the creation of new functionality by adding new code rather than changing existing code. This principle aims to minimize the risk of introducing bugs in existing code when extending the system.
  • Example Explanation: The first set of classes violated OCP by allowing modifications to existing code for each new shape. The corrected example adheres to OCP by using abstract classes and extending them to create new shapes without modifying the existing Shape class.
// Violation of OCP
class Shape {
public double calculateArea() {
// general area calculation logic
}
}

class Circle extends Shape {
// overriding calculateArea for Circle
public double calculateArea() {
// specific area calculation logic for Circle
}
}

// Adhering to OCP
abstract class Shape {
public abstract double calculateArea();
}

class Circle extends Shape {
public double calculateArea() {
// specific area calculation logic for Circle
}
}

class Square extends Shape {
public double calculateArea() {
// specific area calculation logic for Square
}
}

Liskov Substitution Principle (LSP):

  • Definition: Subtypes must be substitutable for their base types without altering the correctness of the program.
  • Explanation: The Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In simpler terms, a derived class should extend the behavior of the base class without changing its behavior.
  • Example Explanation: The first set of classes violated LSP by having a Penguin class that couldn't properly substitute for the Bird class. The corrected example adheres to LSP by ensuring that the Penguin class provides a no-op implementation or behavior consistent with being a bird.
// Violation of LSP
class Bird {
public void fly() {
// general flying behavior
}
}

class Penguin extends Bird {
// violates LSP by not being able to fly
}

// Adhering to LSP
class Bird {
public void fly() {
// general flying behavior
}
}

class Penguin extends Bird {
public void fly() {
// no-op implementation or specific behavior consistent with being a bird
}
}

The phrase “no-op implementation or specific behavior consistent with being a bird” refers to the principle of Liskov Substitution Principle (LSP) in object-oriented programming.

Let’s break it down:

  1. No-op implementation: “No-op” stands for “no operation.” In the context of LSP, it means providing a method implementation in a subclass (like Penguin in the example) that does nothing. For example, if the superclass (Bird) has a method like fly(), the subclass (Penguin) might implement it with an empty body or a return statement that indicates no flying is happening. This allows the subclass to fulfill the contract of the superclass without introducing unexpected behavior.
class Bird {
public void fly() {
// general flying behavior
}
}

class Penguin extends Bird {
public void fly() {
// no-op implementation for a penguin (doesn't actually fly)
}
}

2. Specific behavior consistent with being a bird: Alternatively, if the subclass (Penguin) does provide a specific implementation for the method, it should be behavior consistent with the general concept of being a bird. In the example, a penguin, while unable to fly in the traditional sense, might have a specific swimming behavior that aligns with its bird nature.

class Bird {
public void fly() {
// general flying behavior
}
}

class Penguin extends Bird {
public void fly() {
// specific behavior consistent with being a bird (e.g., swimming)
}
}

Interface Segregation Principle (ISP):

  • Definition: A class should not be forced to implement interfaces it does not use.
  • Explanation: The Interface Segregation Principle (ISP) emphasizes that a class should not be required to implement interfaces it does not need. In other words, classes should not be forced to depend on methods they do not use. This principle encourages the creation of small, specific interfaces to prevent unnecessary dependencies.
  • Example Explanation: The first set of interfaces violated ISP by including methods that were irrelevant to certain classes. The corrected example adheres to ISP by creating separate interfaces (Workable and Eatable) and letting classes implement only the interfaces they need.
// Interface violating ISP
interface Worker {
void work();
void eat();
void sleep();
}

class Robot implements Worker {
public void work() {
// robot-specific work
}

public void eat() {
// no-op implementation for a robot (violates ISP)
}

public void sleep() {
// no-op implementation for a robot (violates ISP)
}
}

class Human implements Worker {
public void work() {
// human-specific work
}

public void eat() {
// human-specific eating behavior
}

public void sleep() {
// human-specific sleeping behavior
}
}

// Adhering to ISP with separate interfaces
interface Workable {
void work();
}

interface Eatable {
void eat();
}

interface Sleepable {
void sleep();
}

class Robot implements Workable {
public void work() {
// robot-specific work
}
}

class Human implements Workable, Eatable, Sleepable {
public void work() {
// human-specific work
}

public void eat() {
// human-specific eating behavior
}

public void sleep() {
// human-specific sleeping behavior
}
}

Dependency Inversion Principle (DIP):

  • Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.
  • Explanation: The Dependency Inversion Principle (DIP) suggests that high-level modules (e.g., business logic) should not depend on low-level modules (e.g., database access). Instead, both should depend on abstractions (e.g., interfaces). This principle helps to decouple components, making the system more flexible and maintainable.
  • Example Explanation: The first set of classes violated DIP by having a high-level module (ReportGenerator) directly depend on a low-level module (DatabaseConnection). The corrected example adheres to DIP by introducing an interface (DatabaseConnector) and having both high-level and low-level modules depend on this abstraction.
// Violation of DIP
class ReportGenerator {
private DatabaseConnection databaseConnection;

public ReportGenerator() {
this.databaseConnection = new DatabaseConnection();
}

public void generateReport() {
// logic to generate a report using databaseConnection
}
}

// Adhering to DIP
interface DatabaseConnector {
// methods related to database connection
}

class DatabaseConnection implements DatabaseConnector {
// implementation of DatabaseConnector
}

class ReportGenerator {
private DatabaseConnector databaseConnector;

public ReportGenerator(DatabaseConnector databaseConnector) {
this.databaseConnector = databaseConnector;
}

public void generateReport() {
// logic to generate a report using databaseConnector
}
}

By following the SOLID principles, developers can build software systems that are more resilient to change, easier to understand, and simpler to extend. While adhering strictly to all the principles may not always be feasible in every situation, understanding and applying them judiciously can lead to better software design and development practices. These principles are particularly relevant in object-oriented programming but can also be adapted to other programming paradigms.

--

--

Saurav Kumar

Experienced Software Engineer adept in Java, Spring Boot, Microservices, Kafka & Azure.