Design Pattern 4-open-closed Principle

Source: Internet
Author: User

Open-closed Principle

It means that software entities (classes, modules, functions, and so on) can be expanded, but cannot be modified.
This principle actually has two features:Open for expansion(Open for extension) ", the other is to say"Changes are closed.(Closed for modification )"
What kind of design can we face the changes in requirements, but it can be relatively stable, so that the system can continue to launch new versions after the first version?
No matter how close the module is, there will be some changes that cannot be closed to it. Since it cannot be completely closed, the designer must choose which change should be closed for the module he designed. He must first guess the most likely types of changes, and then construct an abstraction to isolate those changes.
The open-closed principle means that we should always consider it when designing and try to make this class good enough. If it is well written, we should not modify it. If it comes to new requirements, you can add some classes and the original code will not move.

No matter how close the module is, there will be some changes that cannot be closed to it. Since it is impossible to completely close the module, the designer must choose which changes should be closed for the module he designed. He must first guess the most likely types of changes, and then construct an abstraction to isolate those changes. (Use virtual functions to implement this function)
When we first wrote the code, we assumed that the change would not happen. When a change occurs, we create an abstraction to isolate similar changes that occur later.
For example, the previously written addition program is initially completed in the class where the interface is located, and the change has not yet occurred. Then we add a subtraction function. We send a mail. To add a function, we need to modify the original class. This violates the "open-closed principle". Therefore, we consider restructuring the program, add an abstract operation class to isolate specific addition by some object-oriented means, such as inheritance and polymorphism. The coupling between subtraction and interface can still meet requirements and adapt to changes. In this case, if I add the multiplication and division function, I do not need to change the interface and addition and subtraction classes. Instead, I can add the multiplication and division sub-classes.

In the face of requirements, changes to the program are made by adding new Code, rather than changing the existing code. This is the spirit of the "open-closed 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.