Design Mode-open and closed Principle

Source: Internet
Author: User

Recently, we have seen the shadows of design patterns in many occasions. We have been investing in algorithms and data structures for a long time. It is very important to find the design patterns. Sometimes code maintenance, reuse, and scalability are better than pure algorithms. So I picked up Daniel's book "big talk Design Patterns" and referred to the blogs of various big cows on the Internet to start my design patterns journey.

Today, we will introduce the open and closed mode.

Concept:

Software entities (classes, functions, interfaces, etc.) can be extended, but cannot be modified.

Explanation:Openness refers to scalability, and closures refer to non-modifiable features. When designing an architecture design class, consider the functions that this class may need to complete and the functions to be added in the future. After writing these functions, they will not be modified. If the demand changes, you only need to add and derive other subclasses. The original code can not be modified at all, but it is often impossible. No matter how the module is closed, there will be some changes that cannot be closed with it. Since it is impossible to be completely changed and closed, the designer must make a choice on which changes should the module be designed. He must first abstract the types of changes. Then construct an abstract class to isolate those changes. Assume that there is an addition class that can complete all current functions, but the addition class must be modified when Subtraction is required. This violates the principle of open and closed, you can design an abstract operation class as much as possible, and then the addition and subtraction classes inherit the abstract operation class to avoid modifying the addition. Again, we emphasize that the service can be expanded, but cannot be modified. Alternatively, openness refers to scalability and closed refers to non-modification.

Related Article

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.