The Open closure principle (Ocp,open Closed Principle) is the core of all object-oriented principles. The goal of software design itself is to package change and reduce coupling, and the open closure principle is the most direct embodiment of this goal. Other design principles, many times, serve to achieve this goal.
On the principle of open closure, the core idea is that software entities should be extensible and non-modifiable. In other words, the extension is open, and the modification is closed. Therefore, the open closure principle is mainly embodied in two aspects: open to expansion, meaning that there are new requirements or changes, the existing code can be extended to adapt to the new situation. Enclosing a modification means that once the class is designed, it can do its work independently, rather than making any modifications to the class. "Demand always changes", "No software in the world is constant", these statements are the most classical expression of software requirements. A key point of transmission is that for software designers, it is necessary to achieve flexible system expansion without the need to modify the original system. And how can this be done? Only dependent on abstraction. The core idea of open closure is to abstract programming, not specific programming, because the abstraction is relatively stable. Let the class depend on the fixed abstraction, so the modification is closed, but through the object-oriented inheritance and the polymorphism mechanism, can realize the inheritance of the abstract body, by covering its methods to change the inherent behavior, to implement a new extension method, so for the extension is open. This is the basic idea of implementing the principle of open closure, which is based on two basic design principles, which is the principle of Liskov substitution and synthesis/aggregation reuse. For classes that violate this principle, refactoring is required to improve, and the design patterns commonly used to implement are the template method mode and the strategy mode. Package change is an important means to realize this principle, and encapsulate the state of change frequently as a class.
Open Closure principle OCP