LLD Hub
Learn

Single ResponsibilityOne class, one reason to change

One reason to change per class — split when fixes touch unrelated features. · Small classes are easier to test and reuse.

Watch

Watch, then scroll down for code and practice.

In code

Split responsibilitiesTypeScript
class LogFormatter {
  format(level: string, msg: string) {
    return `[${level}] ${msg}`;
  }
}

class FileLogWriter {
  append(line: string) {
    /* write to disk */
  }
}

class Logger {
  constructor(
    private fmt: LogFormatter,
    private writer: FileLogWriter
  ) {}
  info(msg: string) {
    this.writer.append(this.fmt.format("INFO", msg));
  }
}

📘 Key ideas

The principle

A class should have one, and only one, reason to change. If changing the log format also requires touching the class that sends emails — SRP is violated.

How to spot violations

The class name contains 'And' or 'Manager'. It has more than ~5 public methods. Changes to unrelated features keep touching it.

Fix: extract responsibilities

LogFormatter (formatting), FileHandler (IO), LogFilter (level logic) — each has one job. Logger orchestrates them.

Side effect: testability

SRP classes are trivially unit-testable — they do one thing, so mocking is easy and tests are small.

🧠 Practice — Apply What You Learned

🚀 Now apply what you learned

Pick a problem above, write your solution, and get AI feedback on your design.

Start Practice →