Six principles of design pattern (6) -- open and closed principles

Source: Internet
Author: User

Definition:

The objects (classes, modules, functions, etc.) in the software should be open to extensions, but the modifications should be closed.

Personal Understanding:

In general, the open/closed principle refers to various components contained in the software system, such as modules, classes, and functions. Instead of modifying the existing code, introduce new features. In the open/closed principle, "open" refers to the feature extension that is open to components and allows Function Extension. In the principle of "open/closed ", the modification to the original code is closed, that is, the original code should not be modified.

Problem:

During the software life cycle, some unmodified code may be modified due to changes in requirements, maintenance, and upgrades, in this case, you need to test the unmodified parts.

Solution:

When the software needs to change, try to modify by extending the original entity instead of modifying the original code. That is to say, use abstraction to build the framework and implement extension details.

In fact, the entire definition of the open and closed principle is vague. It only describes how the objects in the software are opened for extensions, but not modified or disabled. The principle of opening and closing is an abstract summary of the first five principles. The first five principles are specific implementations of the principle. Therefore, if the principle of opening and closing is used, it is actually a bit false because it does not have a fixed pattern, however, the ultimate guarantee is to improve the reusability and maintainability of the program.

In connection with the solid (single responsibility principle, open and closed principle, the Lee replacement principle, the interface isolation principle and the dependency inversion principle) in the aforementioned Design Pattern principle, the six principles of design patterns, excluding the principle of open and closed design patterns, are implemented, and the principle of open and closed is also completed. However, it should be clear that the implementation of these principles is not true or false, but the implementation program. The top five principles of the design pattern described earlier have the following functions:

  • The single responsibility principle tells us that the class responsibilities should be single;
  • The Lee's replacement principle tells us not to break down the inheritance of classes;
  • The dependency inversion principle tells us to use interfaces;
  • The interface isolation principle tells us that the interface should be as simple as possible;
  • The dimit law tells us that the coupling between classes should be minimized.

Finally, I will explain how to follow these six principles. The adherence to these six principles is not a question of "yes" or "no", but a question of "more" or "less". That is to say, we generally do not mean whether or not we comply with the six principles, but the degree of compliance. Everything is too late. The same is true for the six design principles of the design model. The purpose of formulating these six principles is not to strictly abide by them, but to use them flexibly according to the actual situation. The degree of compliance with them is well designed as long as they are within a reasonable scope.

Six principles of design pattern (6) -- open and closed principles

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.