"Turn" design mode Six Principles (6): Open and close principle

Source: Internet
Author: User

definition: a software entity such as classes, modules, and functions should be open to extensions and closed for modification.

The problem is: during the software lifecycle, because changes, upgrades, and maintenance are needed to modify the software's original code, errors may be introduced into the old code, or we will have to refactor the entire functionality and require the original code to be re-tested.

Solution: When the software needs to change, try to implement changes by extending the behavior of the software entities, rather than by modifying existing code.

The opening and closing principle is the most basic design principle in object-oriented design, and it guides how to establish a stable and flexible system. The open/Closed principle is probably the most ambiguous one in the six principles of the design pattern, it only tells us that opening to the extension, closing the modification, but how to open the extension, the modification is closed, and does not explicitly tell us. In the past, if someone told me "you must obey the opening and shutting principle when designing," I will feel that he did not say anything, but seems to have said everything. Because the opening and closing principle really taixu.

After careful consideration and careful reading of many design patterns, the principle of closure was finally recognized. In fact, we follow the design pattern in front of the 5 major principles, and the use of 23 design patterns is to follow the opening and closing principle. In other words, as long as we adhere to the previous 5 principles of good, the design of the software is naturally in line with the open and closed principle, the opening and closing principle is more like the front five principles adherence to the degree of "average score", the first 5 principles to abide by the good, average natural high, showing the software design closed-closed principle of good If the previous 5 principles are not well observed, then the open and closed principle is not good.

In fact, I believe that the opening and closing principle is nothing more than to express such a layer of meaning: with the abstract framework, with the implementation of extension details. because the abstract flexibility is good, adaptability is wide, 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 abstract derived implementation class to expand, when the software needs to change, we just need to re-derive an implementation class to expand on demand. Of course, the premise is that our abstraction should be reasonable, to change the needs of the forward-looking and predictable.

Here, recalling the 5 principles mentioned above, is precisely telling us to use abstract constructs the framework, with the implementation of the extension of the details of the matter: the principle of single responsibility tells us that the implementation of the class to be a single duty; the Richter substitution principle tells us not to break the inheritance system. The dependency inversion principle tells us to interface-oriented programming, and the principle of interface isolation tells us to simplify the design of the interface, and the Dimitri Law tells us to reduce the coupling. And the open and closed principle is the master, he tells us to be opened on the extension, close to the modification.

Finally explain how to abide by these six principles. The observance of these six principles is not a question of whether or not, but the question of more and less, that is to say, we generally do not say that there is no compliance, but rather the degree of compliance. Everything is too much, the design pattern of the six design principles is the same, the purpose of formulating these six principles is not to be rigidly abide by them, and need to be flexible to use according to the actual situation. The degree of adherence to them is a good design, as long as it is within a reasonable range. Let's illustrate with a picture.

Each dimension in the diagram represents a principle, and we draw a point in the dimension based on the degree of adherence to this principle, and if it is reasonable to comply with this principle, the point should fall within the red concentric circle, and if the difference is observed, the points will be inside the small circle, and if over-adhered, the points will fall outside the great circle. A good design is shown in the figure, which should be a hexagon with six vertices in the concentric circle.

In, design 1, Design 2 is a good design, their adherence to the six principles are within a reasonable range; Design 3, Design 4 design Although some deficiencies, but also basic acceptable; Design 5 is seriously inadequate, the principles are not well adhered to, and design 6 observes the transition, Design 5 and Design 6 are urgently needed to reconstruct the design.

Here, the six principles of design patterns are written. The main reference books have "design mode" "Design mode of Zen" "Big Talk Design mode" and some scattered articles on the Internet, but the main content is my own on these six principles of sentiment. The purpose of this writing is to systematically collate the six principles and share them with the vast majority of users, because design patterns are really important to programmers. As there is a sentence called 1000 readers in the eyes of 1000 hamlet, if you have a different understanding of these six principles, I welcome the message, we discuss together.

At the same time, in order to facilitate the collection of friends, the following gives word version of the download.

Word version download link: design pattern Six principles

All transferred from http://blog.csdn.net/zhengzhb/article/details/7296944

"Turn" design mode Six Principles (6): Open and close principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.