Software entities (classes, modules, function, etc.) should is open for extension, but closed for modification.
Software entities (modules, classes, methods, etc.) should be open to extensions and closed for modification.
The opening and closing principle (ocp:open-closed principle) refers to the design of a class or other program unit in an object-oriented design (Ood:object oriented), which should follow:
-open to Extension (open)
-close to modification (closed)
Principles of Design.
The opening and closing principle is one of the most basic principles to judge the correctness of object-oriented design.
According to the principle of opening and closing, when designing a software system module (class, method), it should be able to extend its function (extended and open) on the basis of not modifying the original module (modification and shutdown).
-Extended Open: The function of a module is extensible, the module is extended and open. The functional scalability of software systems requires that modules be extended and open.
-Modify Close: A module is called by another module, if the source code of the module is not allowed to modify, the module is modified to close. The functional stability of the software system, the continuity requirement is modified and closed.
This isThe reason why the system design should follow the open and closed principle:
1) stability. The open and closed principle requires that the extension function does not modify the original code, which allows the software system to remain stable in the change.
2) Extensibility. The opening and closing principles require the expansion of open, through the extension to provide new or change the original function, so that the software system has flexible scalability.
The system design that follows the open and closed principle can make the software system reusable and easy to maintain.
The realization method of the opening and closing principle in order to satisfy the open and closed principle of the change-close (closed for modification) principle, as well as the extended openness (open for extension) principle, the invariant parts of the software system should be abstracted, in the object-oriented design,
-These invariant parts can be abstracted into invariant interfaces that can be used to deal with future expansions;
-Minimum functional design principles for interfaces. According to this principle, the original interface can either deal with future extensions, and the insufficient parts can be implemented by defining new interfaces;
-Calls between modules are made through an abstract interface, so that even if the implementation layer changes, there is no need to modify the caller's code.
Interfaces can be reused, but implementations of interfaces are not necessarily reusable. The interface is stable, closed, but the implementation of the interface is variable and open. It is possible to add new or change the original function of the system to realize the soft extension of the software system by the different implementations of the interface and the inheritance behavior of the class.
Simply put, whether the software system has good interface (abstract) design is an important criterion to judge whether the software system satisfies the open and closed principle. Now many of the open and closed principle is equivalent to interface-oriented software design.
The construction of the Relativity software system of the opening and closing principle is a process that needs to be reconstructed continuously. In this process, the function of the module abstraction, the relationship between modules and modules will not be very clear from the beginning, so the construction of 100% to meet the open and closed principle of the software system is very difficult, this is the relative nature of the opening and closing principle. But in the design process, through the function of the module abstraction (interface definition), the relationship between the module abstraction (through interface calls), abstraction and implementation of the separation (interface-oriented design), can be as close as possible to meet the opening and closing principles.