Open-closed Principle
It means that software entities (classes, modules, functions, and so on) can be expanded, but cannot be modified.
This principle actually has two features:Open for expansion(Open for extension) ", the other is to say"Changes are closed.(Closed for modification )"
What kind of design can we face the changes in requirements, but it can be relatively stable, so that the system can continue to launch new versions after the first version?
No matter how close the module is, there will be some changes that cannot be closed to it. Since it cannot be completely closed, the designer must choose which change should be closed for the module he designed. He must first guess the most likely types of changes, and then construct an abstraction to isolate those changes.
The open-closed principle means that we should always consider it when designing and try to make this class good enough. If it is well written, we should not modify it. If it comes to new requirements, you can add some classes and the original code will not move.
No matter how close the module is, there will be some changes that cannot be closed to it. Since it is impossible to completely close the module, the designer must choose which changes should be closed for the module he designed. He must first guess the most likely types of changes, and then construct an abstraction to isolate those changes. (Use virtual functions to implement this function)
When we first wrote the code, we assumed that the change would not happen. When a change occurs, we create an abstraction to isolate similar changes that occur later.
For example, the previously written addition program is initially completed in the class where the interface is located, and the change has not yet occurred. Then we add a subtraction function. We send a mail. To add a function, we need to modify the original class. This violates the "open-closed principle". Therefore, we consider restructuring the program, add an abstract operation class to isolate specific addition by some object-oriented means, such as inheritance and polymorphism. The coupling between subtraction and interface can still meet requirements and adapt to changes. In this case, if I add the multiplication and division function, I do not need to change the interface and addition and subtraction classes. Instead, I can add the multiplication and division sub-classes.
In the face of requirements, changes to the program are made by adding new Code, rather than changing the existing code. This is the spirit of the "open-closed principle.