Opening and closing principles
Definition: A software entity such as classes, modules, and functions should be open to extensions and closed for modification.
The source of the problem: in the software lifecycle, because of changes, upgrades, and maintenance, and other reasons for the software to modify the original code, may introduce errors in the old code, may also cause us to have to refactor the entire function, and the need for the original code to be tested again.
Solution: When the software needs to change, try to make changes by extending the behavior of the software entity, rather than by modifying the existing code.
The principle of opening and closing is the most basic design principle in object-oriented design, which guides us to establish a stable and flexible system. The opening and closing principle may be the most obscure of the six principles of design pattern, it only tells us to open to the extension, to modify the closure, but in the end how to do to open the extension to the modification, and not explicitly tell us. In the past, if someone told me, "you must adhere to the opening and closing principles when you design", I will feel he did not say anything, but seems to have said anything. Because the opening and closing principle is really a void.
After thinking carefully and reading a lot of design patterns, I finally got a little understanding of the principle of closing off. In fact, we follow the design pattern of the top 5 principles, and the use of 23 design patterns is to follow the principle of opening and closing. That is to say, as long as we have complied with the previous 5 principles, design of the software is naturally consistent with the open and closed principle, the open and close principle is more like the front five Principles compliance level of the "average score", the first 5 principles to comply with the good, the average points naturally high, indicating that the software design and closing principles to comply with the good If the previous 5 principles are not good, then the open and closed principle is not adhered to.
In fact, the author believes that the open and closed principle is to express the meaning of such a layer: with the abstract construction framework, with the implementation of extended details. Because of the flexibility of abstraction and wide adaptability, as long as the abstract reasonable, can basically maintain the stability of the software architecture. And the variable details in the software, we use the implementation class derived from the abstract to expand, when the software needs to change, we need to be based on the need to derive a implementation class to expand on it. Of course, the premise is that our abstraction should be reasonable, to be forward-looking and predictable changes in demand.
In this context, recall the 5 principles mentioned above, just to tell us to use the abstract build framework, with the implementation of extension details of the note : a single responsibility principle tells us to implement the class to be a single responsibility; The Richter replacement principle tells us not to break the inheritance system. ; The dependency inversion principle tells us to interface-oriented programming; The interface isolation principle tells us to simplify the design of interfaces; the Dimitri law tells us to reduce coupling. And the opening principle is the general layout, he told us to be open to the expansion of the modification closed.
Finally, explain how to comply with these six principles.
Adherence to these six principles is not a question of whether or not it is a problem, but a matter of many and few, that is, we generally do not say that there is no compliance, but that the degree of compliance. Everything is too much, the design pattern of the six design principles are the same, the purpose of the formulation of these six principles is not to our rigid adherence to them, but need to be flexible in accordance with the actual situation. The degree of compliance with them is a good design as long as it is within a reasonable range.
Let's use a picture to illustrate.
Each dimension of the diagram represents a principle, and we draw a point on the dimension based on the degree of compliance with this principle, if it is reasonable to observe this principle, this point should fall within the concentric circle of red, and if the difference is observed, it will be within the circle; A good design is reflected in the figure, should be six vertices are in concentric circles in the hexagonal.
In the above diagram, design 1, design 2 belong to the good design, their adherence to the six principles is within reasonable bounds; Design 3, Design 4 design Although some deficiencies, but also basically acceptable; Design 5 is seriously inadequate, the principles are not well observed, and design 6 to comply with the transition, Design 5 and Design 6 are all urgently needed to refactor the design. Summary
I recently studied "design mode", inadvertently found on the "design pattern of the six principles," Such a good blog, after reading the feeling of this knowledge clear a lot, here, thank the author's share.
Here, I strongly recommend the original Bo home address: http://blog.csdn.net/zhengzhb/article/details/7296944