Video in progress

Which principle does each letter of the SOLID acronym stand for?

S is Single Responsibility, O is Open/Closed, L is Liskov Substitution, I is Interface Segregation, D is Dependency Inversion. Robert Martin collected these five principles; the goal is code that is easy to change and not brittle.

📖 Answer

It is easy to remember each one in a sentence:

  1. SRP: a class should have only one reason to change.
  2. OCP: adding new behavior should mean writing new code, not editing existing code.
  3. LSP: when a child object is put where a parent is expected, the program shouldn't break.
  4. ISP: don't tie a client to methods it doesn't use; prefer several small interfaces over one big one.
  5. DIP: high-level logic should depend on an abstraction, not on a concrete implementation.

If you've used Spring, you see DIP every day: a service depends on the UserRepository interface, and Spring supplies the concrete implementation.

In an interview, just reciting definitions comes across as weak. Go in with an example from your own project for at least one of them, something like "our ReportService used to do PDF, Excel and email, and we split it into three classes".

SOLID is a guideline, not a law. Adding five interfaces to a small script is needless complexity.