Open-Close principle (ocp:the open-closed Principle)
Open-Close principle: Software entities (classes, modules, functions, etc.) should be extensible, but not modifiable.
The purpose of the design is to maintain the relative stability of the system in the face of changes in demand, which makes it easy to upgrade from one version to another.
Everyone may have this experience, to meet a variety of customers, and the customer's needs are constantly changing, the programmer is such a hard life, all the way to change the past, especially in some non-object-oriented language written code, function parameters become more and more long, the case situation gradually increased, the function becomes very large. Software becomes difficult to understand and maintain after a few years, and the software life cycle seems to end. If you can extend the function of the module, but also do not modify the original test passed the code, that much good!
This is completely achievable, and the key is abstraction. Isolate a possible change by abstracting it. Interface-oriented programming, rather than object-oriented programming, can enhance the flexibility of the program, such as the client class calling the server class, if we want the client object to use a different Server object, You must modify where the server class is used in the client, and if the client calls the server's interface to avoid this modification, simply generate a new interface implementation class and modify the first use of the new subclass, such as main, without needing to modify the client class Using strategy mode and template method mode are the most common methods of meeting OCP.
If you need to adapt to a change, you need to abstract this change, it will increase the complexity of the program. So designers should be familiar with the business and understand customer needs, anticipating the need for abstract changes.
Agile modeling does not suggest a variety of hypothetical changes to be abstracted in advance, but rather when the change takes place the first time it is abstracted, the same change becomes easy. Refactoring the code to maintain a good structure is important, and every abstraction should not make the software more rigid. This is a non-object-oriented language that does not have the advantage
Open-Close principle (ocp:the open-closed Principle)