Skip to content
IRC-CodingIRC-Coding
SOLIDSRPOCPLSPISPDIPClean CodeOOPSoftware Quality

SOLID Principles Basics: SRP, OCP, LSP, ISP, DIP

Learn SOLID principles: Single Responsibility, Open-Closed, Liskov, Interface Segregation, and Dependency Inversion with practical examples.

S

schutzgeist

3 min read
SOLID Principles Basics: SRP, OCP, LSP, ISP, DIP

SOLID Principles Fundamentals

SOLID is an acronym for five object-oriented design principles that lead to maintainable, extensible, and fault-resistant code.

In a Nutshell

SOLID: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, Dependency Inversion.

S — Single Responsibility (SRP)

A class should have only one reason to change.

// Bad: multiple responsibilities
class User {
  saveToDatabase() { /* DB */ }
  sendEmail() { /* Email */ }
  validateEmail() { /* Validation */ }
}

// Good: separated concerns
class User {}
class UserRepository { save(u: User) {} }
class EmailService { send(u: User, msg: string) {} }

O — Open-Closed (OCP)

Open for extension, closed for modification.

// Bad: extend switch for each new type
class DiscountCalculator {
  calculate(type: string): number {
    if (type === 'premium') return 0.8;
    if (type === 'vip') return 0.7;
    return 1.0;
  }
}

// Good: Strategy Pattern
interface DiscountStrategy { getFactor(): number; }
class PremiumDiscount implements DiscountStrategy { getFactor() { return 0.8; } }
class VipDiscount implements DiscountStrategy { getFactor() { return 0.7; } }

L — Liskov Substitution (LSP)

Subtypes must be substitutable for their base types.

// Bad: Penguin cannot fly
class Bird { fly() {} }
class Penguin extends Bird { fly() { throw new Error('Cannot fly'); } }

// Good: correct hierarchy
interface Bird { layEgg(): void; }
interface FlyingBird extends Bird { fly(): void; }

I — Interface Segregation (ISP)

No client should depend on unused methods.

// Bad: fat interface
interface Worker { work(): void; eat(): void; sleep(): void; }

// Good: segregated
interface Workable { work(): void; }
interface Eatable { eat(): void; }

D — Dependency Inversion (DIP)

Depend on abstractions, not concrete implementations.

// Bad: direct dependency
class OrderService {
  private repo = new MySQLDatabase();
}

// Good: Dependency Injection
interface OrderRepository { save(order: Order): void; }
class OrderService {
  constructor(private repo: OrderRepository) {}
}

SOLID at a Glance

PrincipleMeaningGoal
SRPSingle ResponsibilityOne class, one responsibility
OCPOpen-ClosedExtensible without modification
LSPLiskov SubstitutionSubtypes replace base types
ISPInterface SegregationSmall, focused interfaces
DIPDependency InversionAbstraction over concrete types

Key Takeaways for Study

  • SOLID is an acronym for five OOP design principles
  • SRP: one reason to change per class
  • OCP: extend without modifying (Strategy Pattern)
  • LSP: subtypes must honor contracts
  • ISP: many small, specific interfaces
  • DIP: Dependency Injection, abstraction as a dependency

FAQ

1. What does SOLID stand for?

SRP, OCP, LSP, ISP, DIP.

2. What does SRP state?

One class, one responsibility.

3. What does OCP state?

Open for extension, closed for modification.

4. What does LSP state?

Subtypes replace base types.

5. What does ISP state?

Small, focused interfaces.

6. What does DIP state?

Abstractions, not concrete implementations.

7. Who coined SOLID?

Robert C. Martin.

8. Example of LSP violation?

Penguin inherits from Bird but cannot fly.

9. How to achieve OCP?

Strategy Pattern.

10. What is DI?

Injecting dependencies.

11. SOLID vs Clean Code?

SOLID is design; Clean Code includes style.

12. What is a fat interface?

Too many methods; violates ISP.

13. When should you apply SOLID?

As projects grow in scope.

14. Can SOLID be overdone?

Yes; small projects can fall into overengineering.

15. How do SOLID and testing relate?

DIP simplifies mocking; SRP keeps tests focused.

Continue Your SOLID Learning Path

The next article in the SOLID learning path covers Analysis and Design Techniques: UML, GRASP, SOLID and Quality Goals — how UML diagrams, GRASP patterns, and SOLID principles work together in software architecture.

References

  1. https://en.wikipedia.org/wiki/SOLID
  2. https://www.oreilly.com/library/view/clean-code/9780134661742/
  3. https://refactoring.guru/design-patterns/solid-principles

If you’d like to dive deeper into SOLID, Clean Code, and software quality, check out these reads:

Software Engineering

Books about software quality, clean code, code reviews and software development processes

Clean Code: A Handbook of Agile Software Craftsmanship von Robert C. Martin

Clean Code: A Handbook of Agile Software Craftsmanship von Robert C. Martin

Bei Amazon ansehen

Affiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.

The Pragmatic Programmer: Your Journey to Mastery von David Thomas, Andrew Hunt

The Pragmatic Programmer: Your Journey to Mastery von David Thomas, Andrew Hunt

Bei Amazon ansehen

Affiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.

Back to Blog
Share:

Related Posts