Object-oriented principle (I) -- open and closed Principle

Source: Internet
Author: User
The object-oriented principle consists of seven principles: 1. Principle of Open/closed 2. Principle of dependency inversion 3. Principle of single responsibility 4. Principle of replacement by Lee's family 5. Principle of compound reuse 6. Principle of prompt 7. Principle of interface isolation

These principles do not exist in isolation. They are mutually dependent and complementary.

1. Definition of the open/closed Principle: A software entity should be open to extensions and closed to modifications. That is to say, when designing a module, the module should be extended without being modified, that is, the behavior of the module should be changed without modifying the source code.

2. Analysis of the Principle of opening/closing:

(1) the principle of open/closed was proposed by Bertrand Meyer in 1988. It is one of the most important principles in object-oriented design.

(2) in the definition of the open/closed principle, a software entity can refer to a software module, a local structure composed of multiple classes, or an independent class.

(3) abstraction is the key to the principle of openness and closure.

(4) The principle of openness and closure can also be described by a more specific "encapsulation principle of variability" and the principle of variability encapsulation (principle of encapsulation of variation, EVP) it is required to find and encapsulate the variable factors of the system.

3. Example of the open/closed principle:

A graphic interface system provides buttons of different shapes. The client code can program these buttons. Users may need to use different buttons to change their requirements. The original design scheme is as follows:

The system is now restructured to meet the requirements of the open and closed principles.

Comparative Analysis

Figure (1): A client method calls the addition class directly, but I want to add a subtraction class, you will find that you have to change the code in the addition class when adding a subtraction class (implemented using the switch statement), which violates the "Open and Close principle", so we should re-refactor it.

As shown in figure (2), we add a parent class of the operation class in this figure, so that we do not need to modify the client class when adding or subtracting classes.

In the face of requirements, changes to the program are made by adding new Code, rather than changing the original code.

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.