These were put together by the author Robert Martin, also known as Uncle Bob.
- S – Single-responsiblity principle
- O – Open-closed principle
- L – Liskov substitution principle
- I – Interface segregation principle
- D – Dependency Inversion Principle
Single Responsibility
- An object should have one primary responsibility, one reason to exist, and that reason entirely encapsulated within one class.
- It can have multiple behaviors, but all those behaviors are oriented around that core reason for existence, and everything it does should support that.
Open/Closed
- Open for extension but closed to modification.
- After you’ve written some working code, and then your requirements change, if you need to add additional behavior, you do it by writing new code, not by changing old code that already works.
- We’re not changing the existing code that already works, our system is open for extension, but closed for modification
Liskov Substitution
- Objects in a program should be replaceable with instances of their subtypes–their subclasses or derived classes–without altering the correctness of the program.
- It’s an extension of that whole inheritance idea. Meaning that if we’ve created a whole bunch of derived classes or child classes or subclasses–I should always be able to pass any of these around and treat them like their superclass
Interface Segregation
- The idea that many specific interfaces are better than one general purpose interface.
- The idea of having formalized lists of method names that a class can choose to support and implement.
- The principle here is that interfaces, those lists of methods should be as small as possible. And if they start to get bigger, they should be split up into smaller interfaces.
Dependency Inversion
- One should “depend upon abstractions, not concretions.”
- Similar to Dependency Injection.
- It allows substitutions, flexibility going forward, being able to replace and extend without changing the object at all.
Sources:
https://en.wikipedia.org/wiki/SOLID
https://stackoverflow.com/questions/130794/what-is-dependency-injection
Comments