Basic principles of Java Design pattern (II.)

Source: Internet
Author: User

In the previous article, we introduced the principle of single responsibility: http://blog.csdn.net/u010590685/article/details/47011803
Today, let's talk about another principle: the principle of open closure
China has an idiom called maneuvers, of which 捭阖 means to open and close.
"捭阖" is the beginning of "Ghost millet". The with is open and closed. 捭阖, that is, the opening and closing Youdao, relaxation degree. The technique of 捭阖 is the fundamental of the operation of all things in the world, and it is also an important word of the lobbying. Since everything has to be opened and closed, we write code on what open to what close also have rules, this is what we say today to develop the closure principle
Definition: A software entity such as classes, modules, and functions should be open to extensions and closed for modification.
Usually when we do a project, the requirements of the system may change, if the need to change the whole project again is obviously unreasonable and not cost-effective, so we need to use the open closure principle in the design, meet the demand function we through the new class, interfaces, and so on to extend functionality rather than modify the principle so that when our requirements change, we add a class or interface to the changing part of the same, we can change the next time we add an implementation of the interface or inherit this class, so that the code becomes clear and easy to maintain. Let's give an example
For example, just start the need to write the addition program, soon after the completion of the client class, the change does not occur, the need to add a subtraction function, you will find that the added function needs to modify the original class, which violates the open-closed principle, so you should consider the refactoring program, add an abstract operation class, Through some object-oriented means, such as inheritance, dynamic and so on to isolate the specific addition, subtraction and client coupling, the requirements can still be met, but also to cope with change. At this point, the need to add multiplication function, you do not need to change the client and add and subtract classes, but add multiplication and other methods.
But we also have to realize that the absolute closed non-existent, as the ancients said that solitary Yang is not born, solitary Yin is not long. Absolute modification Close is not possible, no matter how the module is ' closed ', there will be some changes can not be closed to it, since it is not possible to completely closed, the designer must be for the module he designed should be a choice of what kind of change closed. He must first guess what kinds of changes are most likely to occur and then construct abstractions to isolate those changes. When we first wrote the code, we assumed that the changes would not occur, and when the changes occurred, we created the abstraction to isolate the changes that happened later.

Copyright NOTICE: This article is the original blog article, reproduced please indicate the source

Basic principles of Java Design pattern (II.)

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.