Learn about SOLID

Tram Ho

Hello everyone, this article I would like to present the knowledge about SOLID that I have learned, I look forward to your follow-up.

1) What is SOLID?

  • SOLID is the five basic principles in object-oriented software design, making the code easier to understand, flexible and maintainable.
  • The author of SOLID is a famous software engineer, Robert C. Martin.
  • SOLID is the following 5 principles in object-oriented design:

1.1) S – Single-responsiblity principle

A class should only have one reason to change, meaning that the class is only responsible for a certain job.

For example

  • This class holds up to 3 responsibilities: Read data from the DB, process data, print results. As long as we change the database, change the way we print the results, we will have to modify this class. Later, the class will become bigger and bigger and harder to manage.
  • In principle, we have to separate this class into 3 separate classes. While the number of classes is greater than the number of repairs it will be simpler, the class is shorter so there are fewer bugs.

1.2) O – Open-closed principle

Do not modify an existing Class, but you can extend it by inheriting

For example

Suppose if you want the sum () method to be able to sum the area of ​​other images as well, then we have to add some more if / else so that it violates the Open-closed principle. To solve the problem we put the sum () function and in the class definition of each image.

This will make the totals easier and more scalable. For example, if you want to calculate the total area with a triangle, we will create a Triangle class for the triangle without changing the calculation class content. However, suppose the object passed to AreaCalculator is not an instance of the image classes and if it is already an image, does it have an area method? To solve this problem we create an interface where the The image class will implement this interface

Then in the AreaCalculator class in sum () method we check if the given image is not an instance of ShapeInterface then fires the error:

1.3) L – Liskov substitution principle

In a program, objects of subclasses can replace the parent class without changing the correctness of the program

For example

  • Result 10 * 10, so the class Square has modified the definition of the parent class Rectangle
  • Looking at the example above, we can see that all calculations are reasonable. Because the square has two equal sides, every time a set length is set, the length of the other side is always set.
  • However, Class Square inherited from Rectangle class, but Square class has other micro images and it changed the behavior of Rectangle class, resulting in LSP violation.
  • In this case, so that the code does not violate the LSP principle, we must create a parent class Shape class, then let Square and Rectangle inherit this Shape class.

1.4) I – Interface segregation principle

Instead of using a large interface, we should split into many small interfaces, with many specific purposes.

For example

For example, if we want to calculate the volume of a shape, we can include it in ShapeInterface:

Now any image created must implement the volume () method, but as we all know, flat geometry will not be able to calculate the volume so they don’t have a volume, so this interface Forcing the Square class to implement a method that does not use it will go against this principle instead we create a new interface called SolidShapeInterface that has a volume method

1.5) D – Dependency Inversion Principle

System components (classes, modules, etc.) should only depend on abstractions, not specific concretions or implementations.

Interface should not depend on details, but vice versa. (Classes communicate with each other through interfaces, not implementations)

For example

We see that the PasswordReminder class is a higher class than the MySQLConnection class that violates D in SOLID because PasswordReminder is forced to depend on the MySQLConnection class. Suppose later you want to change another database, we have to modify the PasswordReminder class so it will violate the Open-closed principle. To solve the problem we create a new interface

This interface has a connect method and the MySQLConnection class implements this interface instead of using MySQLConnection in the contructor of PasswordReminder.

Conclude

  • There are many principles in software design and above are the 5 most important object-oriented design principles (SOLID).
  • Mastering and applying the principles in the design will help our program source code look clearer, take advantage of the advantages of OOP, the components do not depend too much on each other, so that Convenient for later maintenance and expansion.
  • Thank you for watching.
Share the news now

Source : Viblo