Open-Close principle (ocp:the open-closed Principle)

Source: Internet
Author: User

Open-Close principle (ocp:the open-closed Principle)

Open-Close principle: Software entities (classes, modules, functions, etc.) should be extensible, but not modifiable.

The purpose of the design is to maintain the relative stability of the system in the face of changes in demand, which makes it easy to upgrade from one version to another.

Everyone may have this experience, to meet a variety of customers, and the customer's needs are constantly changing, the programmer is such a hard life, all the way to change the past, especially in some non-object-oriented language written code, function parameters become more and more long, the case situation gradually increased, the function becomes very large. Software becomes difficult to understand and maintain after a few years, and the software life cycle seems to end. If you can extend the function of the module, but also do not modify the original test passed the code, that much good!

This is completely achievable, and the key is abstraction. Isolate a possible change by abstracting it. Interface-oriented programming, rather than object-oriented programming, can enhance the flexibility of the program, such as the client class calling the server class, if we want the client object to use a different Server object, You must modify where the server class is used in the client, and if the client calls the server's interface to avoid this modification, simply generate a new interface implementation class and modify the first use of the new subclass, such as main, without needing to modify the client class Using strategy mode and template method mode are the most common methods of meeting OCP.

If you need to adapt to a change, you need to abstract this change, it will increase the complexity of the program. So designers should be familiar with the business and understand customer needs, anticipating the need for abstract changes.

Agile modeling does not suggest a variety of hypothetical changes to be abstracted in advance, but rather when the change takes place the first time it is abstracted, the same change becomes easy. Refactoring the code to maintain a good structure is important, and every abstraction should not make the software more rigid. This is a non-object-oriented language that does not have the advantage


Open-Close principle (ocp:the open-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.