Learn about SOLID principles

Tram Ho

SOLID stands for 5 object-oriented design (OOP) principles. A project that applies these principles will have code that is easy to read, test, and clear. And above all, it helps dev easily maintain and develop the project, specifically as follows:

  • S: S ingle-responsibility principle
  • O: O pen-closed principle
  • L: L iskov substitution principle
  • I: I nterface segregation principle
  • D: D ependency Inversion Principle

Ok, now let me learn more about this principle!

S – Single Responsibility Principle (SRP)

A class should only handle only one job (Only one class can be modified for a single reason).

  • If the code violates this principle – a class handles many tasks:

The OrdersReport class is doing three jobs: retrieving the Order information, reading the Order data from the DB and displaying the Order. When changing the DB or changing how to get data, …, it is imperative that we modify the class. Later, the class will become bigger and bigger, leading to difficult to maintain, difficult to understand, and difficult to clear functions.

When the requirements change, the code refactors, meaning the class is modified. The more tasks a class processes, the more changes are required, and the more difficult those changes will be. So we need to separate the class into separate classes to handle each job.

In this example we need to split as follows

O – Open-Closed Principle

You can extend the class without modifying it inside.

  • This principle is the foundation for building code that is easily maintainable and reusable.

In this shortcut when there is a new request, we can create a new class to extend (by inheriting or owning) the old class without modifying the old class. Let me see the following example:

When we want to calculate the area of ​​a circle, if we change the calculate method in the CostManager class. It will break the Open-closed principle. According to this principle, we cannot modify it, we need to expand it. So how to solve this problem, please follow the code below:

It is now possible to calculate the area without modifying the CostManager class.

L – Liskov Substitution Principle

This principle states that objects must be replaced by an instance of the subtype without changing the proper functioning of the system function.

Imagine you manage two types of coffee machines. According to the user plan, we will use the basic or premium coffee maker, the only difference is that the high-end machine produces better vanilla coffee than the basic machine. The main program behavior must be the same for both machines

I – Interface Segregation Principle

Do not use large interfaces, but instead split into smaller interfaces, with specific purposes.

This principle allows the development of specific interfaces instead of generic interfaces.

Imagine, in the future, you will create a FutureCar that can run and fly ? (imagine a little high), as follows:

The main problem here you can see is that Car and Airplane have the above methods but cannot be used again. The solution is to divide VehicleInterface into two more specific interfaces, which are used only when needed, as follows:

D – Dependency Inversion Principle

A. High level modules should not depend on low level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions. Robert C. Martin

This principle allows programmers to write code separately and easily reuse in many places.

Consider the following example:

In this case, the UserDB class depends directly from the MySQL database. That means if we change the database engine we are using, we need to rewrite this class and this will violate the Open-Closed principle.

The solution is to develop abstraction of database connectivity:

Benefit

Following the SOLID Principles gives us many benefits, they make our system reusable, re-structured, easy to maintain, expand, develop and more.

Above is an article explaining SOLID principles I refer to from articles: SOLID Principles made easy by Dhurim Kelmendi and SOLID Principles Simplified with Examples in PHP by Ideneal.

Hopefully the article will help you improve the quality of your coding process.

Share the news now

Source : Viblo