Understanding the SOLID Principles - Comment

The SOLID principles are great to reduce the possibility of things going wrong, but how can they do it? Well as the reading says, by minimizing class dependencies. These principles are:

  • Single Responsibility Principle 
  • Open/Closed Principle 
  • Liskov Substitution Principle 
  • Interface Segregation Principle 
  • Dependency Inversion Principle
A great example of the Single Responsibility Principle is the usage of microservices. This services have a main purpose and most of the time they only work for that. For example an OCR (Optical Character Recognition) microservice should only accept an input, try to recognize the text inside the image and return a result, nothing less, nothing more. Any variation should be coded outside the microservice to have a different result. An image that can help you understand it is the next one:



For the Open/Closed Principle I think that a good example is the ToString in which you only change the "behavior" of the class you are working on. The ToString will continue working as expected in other Classes and won't change the other methods. An image for this principle is:



In the Liskov Substitution Principle one shouldn't create an object of class A if the object has some different methods that the base class doesn't represent. If you are working with a class Car and the you have a Ferrari car instance and an helicopter comes up, you shouldn't create an Helicopter car instance. Even when this might get you solve your immediate requirement,  this can bring a lot of problems in the future.


Interface Segregation Principle or (ISP) is the one that says that instead of having an interface with a lot of features, divide each of this features in different interfaces by its similar characteristics. In that way we won't have extra not functional features when connecting with an interface.



And in the last one, the Dependency Inversion Principle which says that if you are on need of be dependent of another, make sure it is an interface and not a class because the second tends to change more.



References:

Ortiz, A. (2016). Understanding the SOLID principles. Obtained from:
http://webcem01.cem.itesm.mx:8005/s201713/tc3049/solid.html






Comentarios

Entradas populares de este blog

Metaprogramming (Comment)

Is Design Dead? - Comment