Design principle (1) & quot; open-closed & quot; Principle (OCP)

Source: Internet
Author: User

Design principles (1) "Open-Close" principles (OCP)

The cornerstone of classical mechanics is Newton's three major laws. The first cornerstone of object-oriented reusable design is the so-called Open-Closed Principle (OCP ).

I. What is the principle of opening/closing?

The principle of "open-close" is that a software entity should be open to expansion and closed to modification. This principle was first proposed by Bertrand Meyer:

Software entities shocould be open for extension, but closed for modification.

This principle means that when designing a module, this module should be extended without being modified. In other words, you can change the behavior of this module without having to modify the source code.

Ii. What are the advantages of the open/closed principle?

All software systems share a common nature, that is, their needs change over time. When the software system is facing new requirements, the system design must be stable. The design that meets the "On-Off" principle gives two unparalleled advantages to a software system:

1. By extending existing software systems, new behaviors can be provided to meet new demands for software, so that the software systems in change can have certain adaptability and flexibility.

2. The existing software modules, especially the most important abstract layer modules, cannot be modified any more, which makes the software system in change stable and continuous.

A software system with the above two advantages is a system that achieves reuse at a high level and is easy to maintain.

3. How to comply with the open and closed principles

Let's take a look at how the Jade Emperor maintains the order of Heaven under the challenge of Monkey King.

The Monkey King at the time of the Great noisy tiantium was a new challenge for the Yudi Tiancheng. The Monkey King said: "the emperor will take turns and arrive at my house next year. Just teach him to move out and give me the tiangu !" For this challenge, the advice Venus gave to the Jade Emperor was: "...... Let's send an invitation to announce his upper bound ...... He and his nationality are in Hangzhou ...... One does not move the public worker, and the other accepts xianyoudao ."

In other words, if you do not work hard and do not damage the rules, you will be "closed ". Zhao an's law is the "Opening-closing" principle of Yudi tianting. by assigning a "Ma Wen" official post to Monkey King, the existing system can meet the changing needs, you do not have to change the existing order of heaven, as shown in.

The key to the regulation of security is that it is not allowed to change the existing order of heaven, but it is allowed to include the Demon monkey in the existing order, thus expanding this order. In terms of the face object language, the abstract layer of the system cannot be changed, but the Implementation Layer of the system can be expanded. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + s + authorization + PC9wPg0KPHA + veK + authorization + nP87uvoaPU2s/authorization/K21M/authorization + authorization/bSu7j2u/K24Lj2s + authorization/nT0LXEvt/authorization/ release/release/2z8K2vLK7u + release/release + 38 zlwOC/ydLUuMSx5M + release/release + release/qrfFoaM8L3A + release + PC9wPg0KPHA + release/ycTcu + release + signature/Signature + MaGi0rvW1r/Signature + signature + lda-w90ru49rbuz/PA78PmoaPNrNK71ta/Signature + signature + vB7dK71ta/Signature + DQo8aDIgaWQ9 "IV Conclusion"> 4. Conclusion

Although in many cases, the "open-close" principle cannot be fully implemented, if efforts in this direction can be partially successful, it can also significantly improve the structure of a system.

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.