The open and closed principle OCP (Open-close Principle) is known as the cornerstone of Ood and one of the most important principles of ood.
This principle by the master Bertrand Meyer in 1988 (Khan, at that time I am afraid that few people in the country know Oo, even the computer for what): Software entities should is open for extension,but closed For modification. How simple. This principle is probably meant to say that software should be developed for extensions and should be closed for modification. More popular, that is, we have developed a software that should be able to expand (open) it, and when these extensions are made, there is no need to modify (close) the original program.
Why is there such a requirement? If a software is compliant with the OCP principle, then at least we have two great benefits:
1. Very flexible in software availability. You can expand the software by completing the software and adding new features. This allows the software to meet changing new requirements by constantly adding new modules.
2. Because the original module of the software can not be modified, so do not worry about the stability of the software.
At present, the main one of the implementation of the OCP is abstraction , that is, we often hang on the mouth to face the abstract (interface). The abstraction of all possible behaviors of a system into an abstract underlying, which prescribes the characteristics of the methods that all concrete classes must provide as an abstraction layer of system design, which foresees all possible extensions, so that the abstraction layer of the system does not need to be modified in any extended case At the same time, because one or more new concrete classes can be exported from the abstraction layer to change the behavior of the system, the system design is open to the expansion for the mutable part.
With regard to the variable part of the system, there is also a more specific principle of the variability encapsulation (Principle of encapsulation of Variation, EVP), which is further explained from the principle of the implementation of the project. EVP is required to evaluate and classify all parts of the system that are likely (or allowed) to change as part of the system design, and each variable factor is individually encapsulated.
It is easy to think that it is not possible (and actually uneconomical) to list all possible behaviors of the system at the beginning of the design, and it is not realistic to anticipate and encapsulate all the variables, so the open and close principle is difficult to fully implement, only in some modules, some extent, Meet the requirements of the OCP within a certain limit. So it can be said that the OCP has an idealistic color, is the ultimate goal of Ood. As a result, many of Ood's Masters have struggled to study how the OCP is implemented. The following should be mentioned on the Richter substitution principle, the principle of synthetic reuse, reliance on the reverse principle, the interface isolation principle, abstract class, Dimitri law, etc., can be regarded as the implementation of the OCP method.