Open Closed Principle (OCP)

Open Closed Principle (OCP)


1. Open Closed Principle (Open Closed Principle) is the most basic design principle in the Java world, which guides us how to build a stable and flexible system.
2. Open to extension, closed to modification,

open
means that it can be extended on the basis of source code, such as inheritance, interface, abstract class, etc.
In JAVA, the reason why inheritance is used is to extend its functions under the premise that the class library can be called directly . Development can be done without requiring the user to understand the internal logic of the encapsulated class.


Closed
means that the original packaged code does not allow you to change it, such as the built-in Jar package in the JDK.


Modules designed following the open-closed principle have two main characteristics:
(1) Open for extension. This means that the behavior of modules is extensible. As the needs of the application change, we can extend the module with
new behaviors that meet those changes. That is, we can change the functionality of the module.
(2) It is closed for modification (Closed for modification). When extending the behavior of a module, it is not necessary to change the source code or binary code of the module. The binary
executable , whether it is a linkable library, DLL or .EXE file, does not need to be changed.


Summary:
1. To put it bluntly, it is to open interfaces (an interface with a small function and a small function), inherit others, and abstract the common ones, in order to realize a larger function (implemented by multiple small functions with interfaces).
2. Closure means that the interface function you make should be as simple as possible, and not coupled with other functions, so that there is no possibility of modification.
3. Open is to facilitate function expansion (that is, to write in the form of a functional interface, and other codes can expand new functions through the interface inside)
4. Closure is to encapsulate a part of the interface that will change frequently to deal with changes, that is, whether a lot of code needs to be changed when changing (closing is to reduce such changes)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326981444&siteId=291194637