What is the open-closed principle (OCP)?

The Open / Closed Principle is the most basic design principle in the software development world. It guides us how to build a stable and flexible system.

Definition:

A software entity such as class, module, and function should be open for extension and closed for modification.

The meaning of the principle of opening and closing is that a software entity should be expanded to achieve change, rather than by modifying the existing code to achieve change.

The opening and closing principle is a principle that restricts the current development and design for the future things of the software entity.

Note: The principle of opening and closing is open for extension and closed for modification. It does not mean that no modification will be made. Changes in low-level modules must be coupled with high-level modules, otherwise it will be an isolated and meaningless code fragment.

The importance of the principle of opening and closing:

  • The impact of opening and closing principles on testing

The principle of opening and closing is to keep the original test code still able to run normally, we only need to test the extended code.

  • The principle of opening and closing can improve reusability

In object-oriented design, all logic is combined from atomic logic, instead of implementing a business logic in a class independently. Only in this way can the code be reused. The smaller the granularity, the greater the possibility of being reused.

  • Open and closed principle can improve maintainability
  • Object-oriented development requirements

How to use the opening and closing principle:

First, restrict the extension through the interface or abstract class, limit the extension of the boundary, and do not allow public methods that do not exist in the interface or abstract class;

Second, try to use interfaces or abstract classes for parameter types and reference objects instead of implementation classes;

Third, the abstraction layer should be kept as stable as possible, once it is confirmed, no modification is allowed.

The encapsulation of changes has two meanings:

First, encapsulate the same changes into an interface or abstract class;

Second, encapsulate different changes into different interfaces or abstract classes. Two different changes should not appear in the same interface or abstract class.


Examples

An example, opening and closing doors by hand.

If there is a new demand, it is necessary to increase the function of opening and closing the drawer by hand. For the code, you need to add a Drawer class and modify the Hand class at the same time.

Following the example of the OCP opening and closing principle, opening and closing the door by hand depends on an interface, and the specific implementation class implements this interface.

When it is necessary to increase the number of hands to open and closed the refrigerator, the refrigerator class implements this interface without modifying the original Hand class and interface.

More Object Oriented Resources

Leave a Reply

Twój adres e-mail nie zostanie opublikowany.