Path to Architect (7)-opening and closing principle of OOD

Source: Internet
Author: User

 

2. open-closed principle (OCP)

2.1 What is the principle of open/closed
The principle of opening/closing is the cornerstone of "reusable design" in Object-Oriented Design and one of the most important principles in object-oriented design. Many other design principles are a means to achieve the principle of opening/closing.

In 1988, Bertrand Meyer proposed the open and closed principle in his book object oriented software construction. Its original article is as follows: "software entities shocould be open for extension, but closed for modification ". The translation is: "The software entity should be open to expansion, and the modification should be closed ". This sentence is a little professional. Let's make it more popular, that is, the various components contained in the software system, such as modules and classes) function (functions) and so on, should not modify the existingCodeTo 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.

2.2 how to implement the principle of opening/closing
The key to implementing the open/closed principle lies in "abstraction ". Abstract all possible behaviors of the system into an abstract bottom layer, which specifies the features of all methods required for specific implementation. As the abstraction layer of system design, we need to anticipate all possible extensions so that the underlying abstraction layer of the system does not need to be modified under any extension. At the same time, since one or more new implementations can be exported from the abstract bottom layer and the system behavior can be changed, the system design is open to expansion.

In the process of software development, we have always advocated demand-oriented. This requires us to have a clear understanding of user requirements during the design, determine the possible changes included in the requirements, and then clarify under what circumstances the open/closed principle is used.

There is also a more specific principle of variable encapsulation (principle of encapsulation of variation, EVP ), it further explains the principle of open/closed from the perspective of software engineering implementation. EVP requires the evaluation and classification of all parts of the system that may change during system design. Each variable is encapsulated separately.

In the initial design stage of the actual development process, we need to list all possible behaviors of the system and add these behaviors to the abstract bottom layer. This is simply impossible. It is not economical to do so, time-consuming and labor-intensive. In addition, it is difficult to predict and encapsulate all variables at the beginning of design. Therefore, the Vision depicted by the open and closed principles is only an ideal situation or an extreme state, which is hard to be fully realized in the real world. We can only meet the requirements of the open and closed principle to some extent on some components.

Through the above analysis, we can draw the conclusion that although we cannot achieve blocking, we should try our best to achieve this in system design.

For the function expansion of software systems, we can achieve it by means of inheritance, overloading, or delegation. Taking an interface as an example, it is closed for modification and open for specific implementation. We can provide different implementations based on actual needs, so the interface complies with the open and closed principles.

 

2.3 what are the benefits of the open/closed principle?
If a software system complies with the open/closed principle, it has at least the following benefits from the software engineering perspective:

 High reusability.

After the software is complete, we can still extend the software and add new features, which are very flexible. Therefore, the software system can meet the changing needs by constantly adding new components.

 Good maintainability.

Because the components of the existing software system, especially the abstract underlying layer, are not modified, we do not have to worry about the stability of the original components in the software system, this gives the software system in change stability and continuity.

2.4 relationship between the principle of opening and closing and other principles
The principle of openness and closure is idealistic and is the ultimate goal of object-oriented design. Therefore, the implementation methods of the open and closed principles have always been subject to the painstaking efforts of the object-oriented design masters to study the implementation methods of the open and closed principles. LSP, dip, ISP, abstract class, And interace, can be seen as the implementation method of the open and 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.