Skip to content

Instantly share code, notes, and snippets.

@tientham
Last active April 29, 2019 16:05
Show Gist options
  • Save tientham/db14a3aedd7c2124a32aee308b3a8343 to your computer and use it in GitHub Desktop.
Save tientham/db14a3aedd7c2124a32aee308b3a8343 to your computer and use it in GitHub Desktop.
SOLID principle quick recap

SOLID

  • S - Single Responsibility Principle
  • O - Open/Closed Principle
  • L - Liskov’s Substitution Principle
  • I - Interface Segregation Principle
  • D - Dependency Inversion Principle

S - Single Responsibility Principle

“Class should be having one and only one responsibility”. If a class is going to have multiple features (like: download image, save into database, create new image, v..v..). If in the future, the way we download image changes, then we also may have to change to way we safe image into the database. Or if we are going to have a new database, then we have to change also the way we save the image into the new database as well.

-> In this case, better to have a class with one and one responsibility.

O — Open/Closed Principle

"Classes should be open for extension but closed for modification”. If you want to update a class A which was well written by someone else, then instead of modifying it, you must extend its.

L - Liskov’s Substitution Principle

"Parent classes should be easily substituted with their child classes without blowing up the application”. If we are using a class which extends from other class. Be sure to follow parent class implementation to avoid misused which may risk the system.

I - Interface Segregation Principle

"Many client specific interfaces are better than one general interface”. Instead of having multiple functions inside an interface, try to create multiple interface which is specifed for specific function. For example: Instead of having Interface General with doA() and doB() functions, create 2 interfaces Interface A with doA() and Interface B with doB().

D - Dependency Inversion Principle

"Classes should depend on abstraction but not on concretion" This is to avoid tight coupling. Class A and Class B does not need to know each other but we use the interface (abstraction) for the communication between them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment