http://blog.csdn.net/lovelion/article/details/7537584
In order to satisfy the principle of opening and closing, abstract design is needed for the system, and abstraction is the key to open and close principle. In programming languages such as Java, C #, you can define a relatively stable abstraction layer for the system, and move the different implementation behaviors to the specific implementation layer. In many object-oriented programming languages, interfaces, abstract classes and other mechanisms are provided, which can be used to define the abstraction layer of the system and then extend it through specific classes. If you need to modify the behavior of the system, there is no need to make any changes to the abstraction layer, just add a new concrete class to implement the new business functions, realize the function of extending the system without modifying the existing code, and achieve the requirement of the open and closed principle.
The opening and closing principle is the first cornerstone of object-oriented reusable design, and it is the most important object-oriented design principle. The opening and closing principle was proposed by Bertrand Meyer in 1988 and is defined as follows:
Opening and closing principle (open-closed Principle, OCP): A software entity should open for expansion and close for modifications. That is, the software entity should try to extend without modifying the original code. |
In the definition of open and closed principle, a software entity can refer to a software module, a local structure consisting of multiple classes, or a separate class .
Any software needs to face a very important problem, that is, their needs will change over time. When the software system needs to face the new requirements, we should try to ensure that the system design framework is stable. If a software design conforms to the open and close principle, it is very convenient to extend the system, and without modifying the existing code in the extension, the software system has the adaptability and flexibility to have good stability and continuity. With the increasing of software scale, the longer life of software and the higher cost of software maintenance, it becomes more and more important to design a software system that satisfies the opening and shutting principle.
Open and close principle of object-oriented design principle