OO design principles-the open-closed principle: OO design (OCP) Open/closed principles

Source: Internet
Author: User

The entity (class, module, function, etc.) of a software can be expanded, but cannot be modified.

I. Summary

The open-closed principle: OCP is an object-oriented design. Classes and modules must follow the following rules:

◇ For function expansion, the function can be added because it is open.

◇ Changes are closed, that is, the existing functional entities cannot be modified.

The above two rules mean that "the software entity should be extended without modification"

The open/closed principle (OCP) is the most basic principle of object-oriented design. It is also a later object-oriented design principle,

Package Design Principles and the basis of the design model.

Ii. Advantages of OCP

◇ Excellent scalability. OCP's extended development principles when the system needs to expand or add new behaviors

You only need to add another implementation module.

◇ Provide software stability and stability. The OCP modification and sealing principle should be based on not modifying the existing Code

The append function has little impact on the original function, ensuring the security and stability of the original function.

◇ Reuse. Classes and modules designed according to the OCP principles generally can be used to design abstract and interface reusable functions.

At the same time, the coupling degree of each part of the software will be very low, making program module interchange possible;

Iii. OCP implementation methods

The key to implementing OCP lies in abstraction. The abstracted things remain unchanged. The specific implementation inherits from abstraction, so the close of the changes is ensured,

There are multiple abstract implementation methods that can be added as needed. Of course, this is the open for extension. In addition, the required technologies include,

Multi-state polymorphism, interface, inherits inheritance. The general abstract steps are as follows:

◇ Analysis function.

◇ Define interfaces that can be reused without modification.

◇ Extend and append functions by inheriting interface classes.

 

In addition, pay attention to the following points when encapsulating variability:

◇ Recognition systems may change.

◇ Do not encapsulate a type of code that can be deformed and distributed in multiple places.

◇ Do not mix one variability with another.

◇ The two safe design modes for implementing the open and closed principles are: Strategy pattern ),

Template methord (template method mode ).

◇ Based on the open/closed principle, we try not to modify the class and only extend the class, but in some cases there may be some weird

In this case, several classes can be combined.

◇ Encapsulate the parts that may change into an object, such as status, message, algorithm, and data structure,

Encapsulation changes are an important means to achieve the "open and closed principle", such as the changing status values, such as temperature, pressure, color, points,

Rankings and so on. These can be used as independent attributes. If there is a relationship between parameters, it is necessary to abstract them.

If the behavior remains unchanged, it can be directly used as an object method. Otherwise, abstract or encapsulate these behaviors.

However, due to software uncertainty and other reasons, it is very difficult to design software 100% in accordance with The OCP principle,

Therefore, we should observe the OCP principle as much as possible in the actual process, rather than simply following all the requirements.

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.