Back to OOPs Architecture
Advanced
40 min Read

SOLID Principles

Learning Objectives

  • Single Responsibility
  • Open-Closed Principle
  • Liskov Substitution
  • Interface Segregation
  • Dependency Inversion

SOLID Principles: Scalable Design

SOLID is an acronym for five design principles intended to make software designs more understandable, flexible, and maintainable.

S: Single Responsibility Principle (SRP)

"A class should have one, and only one, reason to change."

  • Bad: A User class that saves itself to the database AND formats its own JSON.
  • Good: A User class for data and a UserRepository for database operations.

O: Open-Closed Principle (OCP)

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

  • Idea: You should be able to add new functionality without touching existing code (using interfaces/abstract classes).

L: Liskov Substitution Principle (LSP)

"Objects of a superclass should be replaceable with objects of its subclasses without breaking the application."

  • Bad: A Square class inheriting from Rectangle where setting width also changes height (violating expectations of a rectangle).

I: Interface Segregation Principle (ISP)

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

  • Idea: Many small, specific interfaces are better than one giant, generic interface.

D: Dependency Inversion Principle (DIP)

"Depend on abstractions, not on concretions."

  • Idea: High-level modules should not depend on low-level modules. Both should depend on interfaces.

Dependency Injection (DI)

DIP is often implemented using Dependency Injection. Instead of a class creating its own dependencies (new Database()), they are "injected" through the constructor.

Confused about this chapter?

Ask our DevVault AI Assistant for instant clarification!

Ask DevVault AI