SOLID – Principle 2: Close and open – Open / Closed principle (OCP)

Tram Ho

Before we dive into the second principle – the Open / Closed principle (OCP), we’ll first look at what SOLID is. (The article simply introduces what SOLID is and names 5 principles of SOLID, to learn more about SOLID and its benefits, readers can read the article in the reference link. Refer or read more in scientific articles, programming books …).

What is SOLID?

SOLID stands for the first 5 letters of the 5 principles of object oriented design. Helps programmers write code that is easy to read, understand, and maintain. It was launched by Robert C. Martin and Michael Feathers. These 5 principles include:

  1. Single responsibility priciple (SRP)
  2. Open / Closed principle (OCP)
  3. Liskov substitution principe (LSP)
  4. Interface segregation principle (ISP)
  5. Dependency inversion principle (DIP)

In this article we will learn about the second principle of SOLID – Open / Closed principle (OCP) – Easy to expand, hard to modify.

Open / Closed principle (OCP) – Close and open

There are two main points in this principle:

  • Modification restrictions: Do not modify the source code of an existing module or class, as it will affect the correctness of the program.
  • Expansion Priority: When adding new features, existing modules / classes should be inherited and extended to larger sub-modules. The modules / subclasses both have the properties of the superclass (which have been properly verified) and have added new features in accordance with the requirements.

Usually, to get more functionality, you have to write more code or fix existing code. However, if you modify the existing code, it will violate content one of this principle. So how can we design a module that is expandable, but difficult to modify?

Before we talk about programming, let’s analyze an actual object. Imagine a dedicated camera, with a lens attached to the camera to capture landscapes, now users want the camera to have more features for portrait photography, instead of bringing out the existing lens to fix them. We extend our camera set by using other lenses, such as those designed for portrait photography. Similarly, the camera has a flash unit but the power is weak, the user wants a higher power lamp … Instead of disassembling and repairing the camera’s parts, we can assemble a separate flash .

The picture below is a camera that is designed in accordance with the Open / Closed principle (OCP). Visibly, the camera is designed to easily expand its functionality without the need to disassemble its internal components. A suitable OCP module should also be designed like that.

Apply principles in programming

Take a look at an example student management program:

Suppose the school has two more types of students: talented students and international students. Talented students receive a 20% discount on the tuition fees, international students an increase of 30% compared to the normal student’s tuition. This has resulted in a change in the tuition fee for students, there are two ways to solve this problem.

The first way is to add a student type attribute to the class SinhVien, and edit the code in the tinhHocPhi () function to produce results suitable for each type of student.

It is easy to see that if done this way, if in the future there are more other types of students, the code must change again, which can lead to new code affecting the old code and damaging the previously run code. OK.

To solve the problem caused by the first way, the second method applies the Open / Closed principle (OCP): modification restriction, extension priority or in other words, the code is designed to be This, even with new requirements, does not have to go back to editing old code. The code is built as follows:

In the future, when there are additional types of students or new requirements, we just need to create an adjacent class from the original SinhVien class without affecting the available classes. Ensure the correctness of the program, limit the scope of testing, help reduce development costs and easier program maintenance.

Conclude

When designing a software, attention should be paid to its maintainability and scalability. When creating classes or modules, always ask the question: is code like this separate? How can this be easily expanded in the future? If yes, how? … That way, we always put a problem to improve our software.

Designing classes based on SOLID principle 2 makes our software much more maintainable and scalable. Of course, it is not always possible to modify existing code, but normally we should not. In short, SOLID is a good guideline to help you design and better program it.

References

Share the news now

Source : Viblo