The principle of opening and closing of design pattern principle

Source: Internet
Author: User

The open-closed principle (OCP) is the cornerstone of "reusable design" in object-oriented design, and is one of the most important principles in object-oriented design, and many other design principles are a means to realize the open-closed principle.

The module designed to follow the open and close principle has two main features: (1) is opened for expansion (open for extension). This means that the behavior of the module can be extended. When the requirements of the application change, we can extend the module to the new behavior that satisfies those changes. In other words, we can change the function of the module. (2) for the modification is off (Closed for modification). When you extend the behavior of a module, you do not have to change the module's source code or the binaries. The binary executable version of the module, whether it is a linked library, DLL, or. exe file, does not have to be changed.

The realization method of opening and closing principle

In order to satisfy the principle of open and closed (closed for modification) and the principle of extended opening (open for extension), the invariant parts of software system should be abstracted, in object-oriented design,
-These immutable parts can be abstracted into immutable interfaces, which can cope with future expansions;
-Minimum functional design principles for the interface. According to this principle, the original interface can deal with the future expansion, and the insufficient part can be realized by defining the new interface;
-Calls between modules are made through an abstract interface, so that the caller's code is not modified even if the implementation layer changes.

The interface can be reused, but the implementation of the interface is not necessarily reusable. The interface is stable, closed, but the implementation of the interface is variable and open. Through the different implementation of the interface and the inheritance of the class of the system to add new or change the original function of the system, to achieve soft expansion of the software system.

To put it simply, whether the software system has good interface (abstract) design is an important criterion to judge whether the software system satisfies the opening and shutting principle. Now many of the open and close principle is equivalent to interface-oriented software design.

Relativity of opening and closing principle

The construction of software system is a process that needs to be reconstructed constantly, in this process, the function abstraction of the module, the relation between module and module, will not be very clear from the beginning, so it is very difficult to construct the software system which satisfies the opening and closing principle, which is the relativity of the open and closed principle. But in the design process, through the abstraction of the function of the module (interface definition), the abstraction of the relationship between modules (through the interface call), abstraction and implementation separation (interface-oriented programming), etc., can be as close as possible to meet the opening and closing principle.

The principle of opening and closing of design pattern 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.