6. Opening and closing principle (Open Closed PRINCIPLE,OCP)
6.1 definition
(1) A class should be open to the extension and closed for modification . It is required to implement the change by extension, and it is extensible without modifying its own code , and does not have to change its own source code or binaries.
(2) in the software life cycle, change is an established fact , in the design to adapt to these changes as far as possible to improve the stability and flexibility of the item, truly " embrace change ." The open and closed principle tells us to implement these changes by extension rather than modifying the original code.
(3) Modification is closed, does not mean that the software does not make any changes, low-level module changes, it is necessary to the high-rise module coupling, otherwise is an arc of meaningless code fragments.
6.2 Advantages of opening and closing principle
(1) by extending its own functionality, new behaviors can be provided to meet the new requirements of the software, adapting and flexibility of the changing software system.
(2) Existing software modules, especially the most important abstraction layer module can not be modified, which makes the software in change have some stability and continuity
(3) also meet the reusability and maintainability
6.3 How to use the open and closed principle
(1) Abstract constraints
① bounds the extension by means of an interface or abstract class constraint extension. through an interface, only the methods provided by the interface can be used .
② parameter types, referencing objects try to use interfaces or abstract classes instead of specific implementation classes
③ the abstraction layer as far as possible to maintain stability , once determined do not modify.
(2) Meta data (metadata) control module behavior
Metadata is the data used to describe the environment and data. Try to use metadata to control program behavior and reduce duplication of development.
(3) Development of the Project Charter
(4) Package changes
① encapsulate the same changes into an interface or abstract class; changes should not be scattered in many corners of the code, but should be encapsulated in an object. A different representation of the same change implies that it is a specific subclass in the same inheritance hierarchy.
② package different changes into different interfaces or abstract classes, there should not be two different changes appearing on the same interface or abstract class. A change mixed with another change would violate the principle of singleness and therefore should be separated.
6.4 Best Practices
(1) Summary of several design principles (6 Principles of the first letter combination of words (S.O.L.I.D, indicating stability)
① Single Duty principle tells us that the realization of the class to be a single responsibility ;
② the Richter replacement principle tells us not to destroy the inheritance system ;
The ③ dependency inversion principle tells us to interface-oriented programming ;
The ④ interface Isolation principle tells us to streamline a single interface when designing interfaces ;
The ⑤ Dimitri Law tells us to reduce coupling .
The ⑥ open and close principle is the master, he tells us to be opened on the extension, closed for modification .
(2) To establish a stable, flexible and robust design, the opening and closing principle is the most basic principle, but also the spiritual leader
(3) The opening and closing principle is the most basic principle, the first five principles are the specific form of the open and close principle, and the opening and closing principle is the soul. In other words, the open and closed principle is abstract class, and the other five principles are concrete implementation classes.
2nd. Object-oriented design principle (SOLID): 6_ Open and closed principle