Solid principles

SOLID principles are a set of five design principles in object-oriented programming that help create scalable, maintainable, and flexible software systems. They were introduced by Robert C. Martin (Uncle Bob) and are widely used in software design.


1. S - Single Responsibility Principle (SRP)

A class should have only one reason to change.

Explanation: Each class should have only one job or responsibility. If a class has multiple responsibilities, changes in one part might affect the other.

Example (Bad Design - Violating SRP)

class Report {
    void generateReport() {
        System.out.println("Generating report...");
    }

    void printReport() {
        System.out.println("Printing report...");
    }
}

Here, the Report class has two responsibilities:

  1. Generating the report

  2. Printing the report

If we need to change the way reports are printed, we also modify the same class, which is against SRP.

Good Design (Following SRP)

Now, changes in report generation won’t affect report printing.


2. O - Open/Closed Principle (OCP)

Software entities should be open for extension but closed for modification.

Explanation: A class should allow its behavior to be extended without modifying its source code.

Example (Bad Design - Violating OCP)

If we add a new payment method (e.g., Bitcoin), we have to modify the existing class.

Good Design (Following OCP)

Now, if we need to add Bitcoin payment, we can create a new BitcoinPayment class without modifying existing code.


3. L - Liskov Substitution Principle (LSP)

Subtypes must be substitutable for their base types.

Explanation: A subclass should be able to replace its superclass without altering the correctness of the program.

Example (Bad Design - Violating LSP)

Here, Square is a subclass of Rectangle, but it breaks LSP because changing the width also changes the height, making it behave differently from a rectangle.

Good Design (Following LSP)

Now, both Rectangle and Square can be used interchangeably without breaking behavior.


4. I - Interface Segregation Principle (ISP)

A client should not be forced to depend on methods it does not use.

Explanation: Instead of having large interfaces, break them into smaller, more specific ones.

Example (Bad Design - Violating ISP)

Good Design (Following ISP)

Now, Robot is not forced to implement eat().


5. D - Dependency Inversion Principle (DIP)

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

Explanation: Instead of directly depending on concrete implementations, depend on abstractions (interfaces).

Example (Bad Design - Violating DIP)

Here, Application is tightly coupled to MySQLDatabase, making it hard to switch to another database.

Good Design (Following DIP)

Now, Application depends on the Database interface, making it flexible to use any database.


Summary of SOLID Principles

Principle
Description
Example Issue

S - Single Responsibility

A class should have one responsibility.

A class handling both report generation and printing.

O - Open/Closed

A class should be open for extension but closed for modification.

Adding new payment methods requiring modification of existing code.

L - Liskov Substitution

Subtypes must be substitutable for their base types.

A Square class breaking Rectangle’s behavior.

I - Interface Segregation

Clients should not be forced to depend on unused methods.

A Robot being forced to implement an eat() method.

D - Dependency Inversion

Depend on abstractions, not concrete classes.

An Application class directly depending on MySQLDatabase.

Following SOLID principles leads to a well-structured, maintainable, and scalable codebase. 🚀

  • Python

Here are the SOLID principles explained with Python examples. 🚀


1. Single Responsibility Principle (SRP)

A class should have only one reason to change.

Bad Design (Violating SRP)

👎 Problem: The Report class is responsible for both generating and printing the report. If we need to change the way we print, we have to modify this class.

Good Design (Following SRP)

👍 Fix: Now, each class has a single responsibility, making the system more modular and easier to maintain.


2. Open/Closed Principle (OCP)

Software entities should be open for extension but closed for modification.

Bad Design (Violating OCP)

👎 Problem: If we add a new payment method (e.g., Bitcoin), we need to modify this class.

Good Design (Following OCP)

👍 Fix: Now, if we need to add Bitcoin, we just create a new BitcoinPayment class without modifying existing code.


3. Liskov Substitution Principle (LSP)

Subtypes must be substitutable for their base types.

Bad Design (Violating LSP)

👎 Problem: A Square should not behave like a Rectangle. Setting width also changes the height, breaking expected behavior.

Good Design (Following LSP)

👍 Fix: Now, Square and Rectangle can be used interchangeably without breaking behavior.


4. Interface Segregation Principle (ISP)

A client should not be forced to depend on methods it does not use.

Bad Design (Violating ISP)

👎 Problem: The Robot class is forced to implement eat(), even though it doesn’t make sense.

Good Design (Following ISP)

👍 Fix: Now, Robot implements only what it needs.


5. Dependency Inversion Principle (DIP)

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

Bad Design (Violating DIP)

👎 Problem: The Application class is tightly coupled to MySQLDatabase. Switching to another database requires modifying the application.

Good Design (Following DIP)

👍 Fix: Now, Application depends on the Database interface, making it easy to switch databases.


Summary of SOLID Principles in Python

Principle
Description
Example Issue

S - Single Responsibility

A class should have one responsibility.

A class handling both report generation and printing.

O - Open/Closed

A class should be open for extension but closed for modification.

Adding new payment methods requiring modification of existing code.

L - Liskov Substitution

Subtypes must be substitutable for their base types.

A Square class breaking Rectangle’s behavior.

I - Interface Segregation

Clients should not be forced to depend on unused methods.

A Robot being forced to implement an eat() method.

D - Dependency Inversion

Depend on abstractions, not concrete classes.

An Application class directly depending on MySQLDatabase.

Following SOLID principles leads to a well-structured, maintainable, and scalable codebase. 🚀🔥

Last updated