Definition of the open close principle

Definition of the open closed principle: a software entity such as classes, modules, and functions should be open for extension and closed for modification. That is, a software entity should make changes through extensions, not through modifications to existing code.

The software entity includes the following sections

  1. Modules in a project or software product that are divided according to certain logical rules
  2. Abstract and class
  3. methods

Note: the open closed principle is open for extension and closed for modification, but it does not mean that no modification is made. The change of the underlying module must be coupled by the high-level module, otherwise it is an isolated and meaningless code fragment

Benefits of the open close principle

The open and closed principle is the most basic principle, the other five principles are the specific form of the open and closed principle, is the tool and method to guide the design, and the open and closed principle is the spiritual leader.

  1. The open close principle is good for unit testing
  2. The open close principle improves reusability
  3. The open close principle improves maintainability
  4. Requirements for object-oriented development

Use of the open close principle

1. Abstract constraints

  1. The extension is bound by an interface or abstract class, which does not allow public methods that do not exist in the interface or abstract class
  2. Try to use interfaces or abstract classes rather than implementation classes for parameter types and reference objects
  3. The abstraction layer should be as stable as possible, and should be implemented as soon as it is determined that interface changes are not allowed or as soon as abstract classes are defined, without the thought of modifying interfaces unless it is a complete rework

2. Control the behavior of the metadata module

Metadata: Data used to describe the environment and data, commonly known as configuration parameters.

By extending a subclass and modifying the configuration file, the business changes are accomplished and the framework benefits

3. Formulate project charter

4. Encapsulate change

The encapsulation of change has two implications:

  1. Encapsulate the same changes into an interface or abstract class
  2. Encapsulate different changes into different interfaces or abstract classes

You cannot have two different changes in the same interface or abstract class. Encapsulate change, identify points of expected change or instability, create stable interfaces for those points of change, and precisely encapsulate change that is likely to occur. The 23 design patterns encapsulate change from different perspectives.


  1. The open and close principle is just one principle. There are many ways to embrace change, not limited to these six design principles, but following these six design principles can deal with most of the changes.
  2. Project regulations are very important
  3. Anticipate change. In practice, once it is found that change is possible or has occurred, it is necessary to consider whether the existing architecture can easily implement the change.

The open closed principle is an ultimate goal that no one, including masters, can achieve 100%, but working in this direction can significantly improve the architecture of a system and truly “embrace change “.