. NET Senior Architect Architect's Road (5)---opening and closing principle

Source: Internet
Author: User

2 opening and closing principle (open-closed PRINCIPLE,OCP)


2.1 What is the open and closed principle
The opening and closing principle is the cornerstone of "reusable design" in object-oriented design, and is one of the most important principles in object-oriented design, and many other design principles are a means to realize the open and closed principle.


In 1988, Bertrand Meyer in his book "Object Oriented Software construction" proposed the opening and closing principle, its original text is like this: "Software entities should is open for Extension,but Closed for modification ". The translation is: "Software entities should be open to the extension, the modification is closed." This is a little bit more professional, we make it more popular, that is: The software system contains a variety of components, such as modules (Modules), classes (Classes) and Functions (Functions), and so on, should not modify the existing code on the basis of the introduction of new features. "Open" in the principle of opening and shutting, refers to the expansion of the function of the component is open, is allowed to expand its function, the opening and closing principle of "closed" refers to the original code modification is closed, that is, the original code should not be modified.


2.2 How to realize the opening and shutting principle
The key to realizing the opening and shutting principle is "abstraction". The abstraction of all possible behaviors of a system into an abstract bottom layer prescribes the characteristics of the methods that all concrete implementations must provide. As the abstraction layer of system design, it is necessary to foresee all possible extensions so that the abstraction of the system does not need to be modified at any scale, and because one or more new concrete implementations can be exported from the abstraction level, the system's behavior can be changed, so the system design is open to the extension.


We are always advocating demand-oriented in the process of software development. This requires us to be very clear about the user's needs when designing, to judge the possible changes that are contained in the requirements, and to identify the circumstances under which the open/closed principle is used.


With regard to the variable part of the system, there is also a more specific principle of the variability encapsulation (Principle of encapsulation of Variation, EVP), which is further explained from the point of view of the Software engineering implementation of the principle of closure. EVP is required to evaluate and classify all parts of the system that may have changed during system design, and each variable factor is individually encapsulated.


At the beginning of the design of the actual development process, we should list out all possible behaviors of the system, and add these behaviors to the abstract bottom, it is impossible, so it is not economical, time-consuming and laborious. In addition, it is difficult to anticipate and encapsulate all the variables at the beginning of the design process. Therefore, the open and closed principle depicts the vision is only an ideal situation or extreme state, the real world is difficult to be fully realized. We can only in certain components, to some extent, in line with the requirements of the open and closed principle.


Through the above analysis, for the opening and closing principle, we can draw the conclusion that although we could not be completely closed, but in the system design, we should try to do this.


For the function extension of software system, we can realize it by means of inheritance, overloading or entrusting. Taking the interface as an example, it is closed to the modification, and the concrete implementation is open, we can provide different implementations according to the actual needs, so the interface is in line with the principle of opening and shutting.


2.3 What are the benefits of the open/closed principle?
If a software system conforms to the open and closed principle, then from the software engineering point of view, it has at least such benefits:


? Reusability is good.

We can still extend the software, add new features, and be flexible when the software is finished. As a result, the software system is able to meet changing needs by continually adding new components.


? good maintainability.

Because of the components of the existing software system, especially its abstract bottom-up is not modified, so we do not have to worry about the stability of the original components of the software system, which makes the change of software system has a certain stability and continuity.


2.4 The relationship between opening and closing principle and other principles
The opening and closing principle has the idealistic color, it is the object-oriented design ultimate goal. Therefore, aiming at the realization method of open and closed principle, the master of object-oriented design has been making great pains to study the realization mode of open and close principle. The following reference to the Richter substitution principle (LSP), the dependency reversal principle (DIP), the interface Isolation principle (ISP) and the abstract class (Abstraction Class), interface (Interace), and so on, can be seen as the implementation of the closed-open principle.

. NET Senior Architect Architect's Road (5)---opening and closing 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.