Open and close principle of object-oriented design principle

Source: Internet
Author: User

http://blog.csdn.net/lovelion/article/details/7537584

In order to satisfy the principle of opening and closing, abstract design is needed for the system, and abstraction is the key to open and close principle. In programming languages such as Java, C #, you can define a relatively stable abstraction layer for the system, and move the different implementation behaviors to the specific implementation layer. In many object-oriented programming languages, interfaces, abstract classes and other mechanisms are provided, which can be used to define the abstraction layer of the system and then extend it through specific classes. If you need to modify the behavior of the system, there is no need to make any changes to the abstraction layer, just add a new concrete class to implement the new business functions, realize the function of extending the system without modifying the existing code, and achieve the requirement of the open and closed principle.

The opening and closing principle is the first cornerstone of object-oriented reusable design, and it is the most important object-oriented design principle. The opening and closing principle was proposed by Bertrand Meyer in 1988 and is defined as follows:

Opening and closing principle (open-closed Principle, OCP): A software entity should open for expansion and close for modifications. That is, the software entity should try to extend without modifying the original code.

In the definition of open and closed principle, a software entity can refer to a software module, a local structure consisting of multiple classes, or a separate class .

Any software needs to face a very important problem, that is, their needs will change over time. When the software system needs to face the new requirements, we should try to ensure that the system design framework is stable. If a software design conforms to the open and close principle, it is very convenient to extend the system, and without modifying the existing code in the extension, the software system has the adaptability and flexibility to have good stability and continuity. With the increasing of software scale, the longer life of software and the higher cost of software maintenance, it becomes more and more important to design a software system that satisfies the opening and shutting principle.

Open and close principle of object-oriented design 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.