Java and mode Reading Notes 3-"Open-Close" principle (OCP)

Source: Internet
Author: User

The first cornerstone of object-oriented reusable design (OOD) is the "open-closed" principle (OCP ).

The principle of opening/closing is that a software entity should be open to extensions and closed to modifications. This principle means that when designing a module, the module should be extended without being modified. In other words, you should be able to change the behavior of this module without having to modify the source code.

The design that meets the "open-closed" principle can bring two incomparable advantages to a software system:

* By extending the existing software system, new behaviors can be provided to meet new requirements for the software, so that the software system in change has certain adaptability and flexibility.

* Existing software modules, especially the most important abstraction layer modules, cannot be modified, 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.

From another perspective, the principle of opening and closing is the so-called "encapsulation principle of variability (EVP )". This principle is to find a variable factor in the system and encapsulate it.

Encapsulation principles for variability mean:

1. A variability should not be scattered in many corners of the code, but should be encapsulated into an object. Different representations of the same variability mean that specific sub-classes in the same hierarchy are inherited.

2. One variability should not be mixed with another.

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.