Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 57 ยท Design Principles

SOLID at a Glance

A tour of the five SOLID principles: why they exist, how they relate to design patterns, their trade-offs, and a runnable example of code that breaks several at once.

Patterns tell you what to build for a recurring problem. Principles tell you why some designs age well and others rot. The best-known set is SOLID, five guidelines popularized by Robert C. Martin and collected under that acronym by Michael Feathers. They are not laws. They are heuristics that point at the same underlying goal: code where a change in one place does not force changes in ten others.

This lesson is a map. Each principle gets its own lesson later in this chapter, with a before-and-after refactor. Here you will learn what each one says in plain words, how they fit together, where they cost something, and how to notice a violation in code you can run.

SOLID in five lines

Single responsibility One reason to change.
Open/closed Extend without editing.
Liskov substitution Subtypes keep promises.
1 / 3

The five principles in plain words

S, Single Responsibility. A class should have one reason to change. If the same class changes when the tax rules change and when the report layout changes, it has two responsibilities. See Single Responsibility Principle.

O, Open/Closed. Code should be open for extension and closed for modification: add behavior by adding new code, not by editing tested code. Typically achieved through polymorphism. See Open/Closed Principle.

L, Liskov Substitution. Anywhere you use a parent type you should be able to use a subtype without surprises. A subclass must keep the promises of its parent. See Liskov Substitution Principle.

I, Interface Segregation. Clients should not be forced to depend on methods they do not use. Prefer several small role interfaces to one fat one. See Interface Segregation Principle.

D, Dependency Inversion. High-level policy should not depend on low-level detail; both should depend on abstractions. See Dependency Inversion and Dependency Injection.

How they fit together

The five principles support each other, and one violation often causes another.

  • Dependency Inversion makes Open/Closed possible: if the high-level code depends on an abstraction, you can add a new implementation without editing it.
  • Open/Closed only works if subtypes obey Liskov; otherwise substituting a new subtype breaks callers.
  • Interface Segregation keeps abstractions small so that implementations are not forced to fake methods, which would break Liskov.
  • Single Responsibility keeps each piece small enough that the other four are practical.
flowchart LR
    SRP["Single responsibility<br/>small focused parts"] --> ISP["Interface segregation<br/>small contracts"]
    ISP --> LSP["Liskov substitution<br/>honest subtypes"]
    LSP --> OCP["Open/closed<br/>extend by adding"]
    DIP["Dependency inversion<br/>depend on abstractions"] --> OCP

Smaller parts and honest contracts are what make "extend by adding" safe.

How they relate to patterns

Most classic patterns are applications of these principles. Strategy and Decorator are ways to satisfy Open/Closed. Factory Method and Abstract Factory help Dependency Inversion by hiding which concrete class is created. Adapter lets you put a stable abstraction in front of an unstable detail. Command and Observer reduce coupling between sender and receiver. When you meet a pattern, ask which principle it is serving; that is usually the reason it exists.

Code that breaks the principles

Here is a small order-processing class. It works, and it breaks nearly every principle at the same time. Read it, then run it.

JS A class that violates SOLID

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Count the reasons to change: validation rules, pricing rules, database, notification text. That is four jobs in one method.

  • S: four responsibilities in one class.
  • O: a new customer type means editing process.
  • D: the class talks directly to a concrete database and email, so you cannot test it without them.
  • L and I do not appear here only because there is no inheritance and no interface yet. They show up as soon as you try to fix it badly, for example by subclassing OrderProcessor for each customer type.

A first, small repair

You do not need all five lessons to see the direction. Pull the pieces apart and pass the collaborators in.

JS The same behavior, split into roles

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output matches the first version, but now a new customer type is one new entry in discountFor, and the processor can be tested with fakes. Later lessons take each of these moves apart and name them properly.

Trade-offs: SOLID is not free

Applying the principles mechanically has costs.

  • More files and indirection. A 30-line class can become six small ones. Reading the flow takes more jumps.
  • Premature abstraction. An interface with one implementation, created "in case", is often noise. Prefer to abstract when a second case actually appears.
  • Misapplied Single Responsibility. "One reason to change" is about actors and axes of change, not "one method per class". Splitting too far scatters related logic.
  • Different languages need different amounts. In JavaScript or Python, a function can be a strategy, so you may not need an interface at all.

A sensible habit: write straightforward code first, and refactor toward SOLID when you feel change pain, such as a function you are afraid to edit or a class you cannot test.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which principle says a class should have only one reason to change?

    Single Responsibility is about having one axis of change, such as only the pricing rules or only the report layout.
  2. 2. Adding a new customer type forces you to edit an existing if/else chain in tested code. Which principle is most clearly violated?

    Open/Closed asks that new behavior be added by writing new code, not by modifying existing code.
  3. 3. Why does a class that calls a concrete database class directly make testing hard?

    That is a Dependency Inversion problem: the high-level code is welded to a low-level detail, so a test cannot swap in a fake.
  4. 4. Which is a reasonable warning about applying SOLID?

    SOLID is a set of heuristics. Applied mechanically, it can produce needless abstraction. Refactor toward it when change pain appears.

Summary

  • SOLID stands for Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion.
  • The principles all serve one goal: make change cheap by lowering coupling and raising cohesion.
  • They reinforce each other: dependency inversion enables open/closed, which relies on honest subtypes and small interfaces.
  • Many patterns (Strategy, Decorator, Factory, Adapter) are applications of these principles.
  • The cost is more structure; apply them when change hurts, not by reflex.

Next, start with the first principle: Single Responsibility Principle.