What does the open-close principle mean?

藏色散人
Release: 2023-04-05 20:26:01
forward
7674 people have browsed it

Opening and Closing Principle

The Opening and Closing Principle (OCP) is the cornerstone of "reusable design" in object-oriented design and one of the most important principles in object-oriented design. Many other design principles are a means of realizing the open-closed principle.

In 1988, Bertrand Meyer proposed the opening and closing principle in his book "Object Oriented Software Construction". Its original text is as follows: "Software entities should be open for extension, but closed for modification”. Translated: "Software entities should be open to extension and closed to modification." This sentence is a little bit professional, let’s put it more informally, that is: the various components included in the software system, such as modules (Modules), classes (Classes), functions (Functions), etc., should be Modify existing code and introduce new features. The "open" in the open-closed principle means that the expansion of the component's functions is open, and functional expansion is allowed; the "closed" in the open-closed principle means that the modification of the original code is closed, that is, it cannot The original code should be modified.

Modules designed following the opening and closing principle have two main characteristics:

(1) Open for extension. This means that the module's behavior is extensible. When the needs of the application change, we can extend the module to have new behaviors that meet those changes. That is, we can change the functionality of the module.

(2) Closed for modification. When extending a module's behavior, you do not have to change the module's source code or binary code. The binary executable version of the module, whether it is a linkable library, DLL or .EXE file, does not need to be modified.

Implementation method

The key to realizing the opening and closing principle lies in "abstraction". Abstract all possible behaviors of the system into an abstract bottom layer, which specifies the characteristics of all methods that must be provided by specific implementations. As the abstraction layer of system design, it is necessary to foresee all possible extensions, so that in any expansion case, the abstract bottom layer of the system does not need to be modified; at the same time, because one or more new concrete implementations can be derived from the abstract bottom layer, the system can be changed behavior, so the system design is open to extension.

We have always advocated demand orientation in the process of software development. This requires us to have a very clear understanding of user needs when designing, judge the possible changes contained in the needs, and thus clarify under what circumstances the opening and closing principle should be used.

Regarding the variable part of the system, there is also a more specific Principle of Encapsulation of Variation (EVP), which further explains the opening and closing principle from the perspective of software engineering implementation. . EVP requires that when designing a system, all parts of the system that may change should be evaluated and classified, and each variable factor should be individually encapsulated.

In the initial design stage of the actual development process, we must list all possible behaviors of the system and add these behaviors to the abstract bottom layer. It is simply impossible, and it is also uneconomical to do so. Therefore, we should realistically accept modifications and embrace changes, so that our code can be open to extensions and closed to modifications.

Benefits

If a software system complies with the opening and closing principle, then from a software engineering perspective, it at least has the following benefits:

Good reusability.

After the software is completed, we can still expand the software and add new functions, which is very flexible. Therefore, this software system can meet changing needs by continuously adding new components.

Good maintainability.

Since the components of the existing software system, especially its abstract bottom layer, will not be modified, we do not need to worry about the stability of the original components in the software system, which makes the changing software The system has certain stability and continuity.

The above is the detailed content of What does the open-close principle mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:hcoder.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template