Open closed principle (OCP):
A software entity such as classes, modules and functions should be open to extensions and closed to modifications.
Build the framework with abstraction and expand the details with implementation
Advantages: improve the reusability and maintainability of the software system
Dependency Inversion Principle (DIP): high-level modules should not rely on low-level modules, and both should rely on their abstraction
Abstractions should not rely on details; Details should rely on abstraction
Programming for interfaces, not for implementations
Advantages: it can reduce the coupling between classes, improve system stability, improve code readability and maintainability, and reduce the risk caused by program modification
Single responsibility principle (SRP): there should be no more than one reason for class change
A class / interface method is responsible for only one responsibility
Advantages: reduce the complexity of the class, improve the readability of the class, improve the maintainability of the system and reduce the risk caused by the change
Interface Segregation Principle (ISP): use multiple special interfaces instead of a single general interface. The client should not rely on the interfaces it does not need
The dependence of a class on a class should be based on the smallest interface
Establish a single interface, not a huge and bloated interface
Refine the interface as much as possible and minimize the methods in the interface (pay attention to the principle of moderation and be moderate)
Advantages: it conforms to the design idea of high cohesion and low coupling
Thus, the class has good readability, scalability and maintainability.
Law of Demeter (LOD):
One object should have minimal knowledge of other objects. Also known as the least known principle
Minimize the coupling between classes
Emphasize only communicating with friends and not talking to strangers
The classes that appear in the input and output parameters of member variables and methods are called member friend classes,
The class that appears inside the method body does not belong to the friend class.
Advantages: reduce coupling between classes
Liskov Substitution Principle (LSP): inheritance must ensure that the properties owned by the superclass are still valid in the subclass
Richter’s substitution principle mainly expounds some principles about inheritance, that is, when inheritance should be used, when inheritance should not be used, and the principles contained therein. Richter substitution is the basis of inheritance and reuse. It reflects the relationship between base class and subclass, complements the opening and closing principle, and standardizes the specific steps of abstraction.
Advantages: strengthen the robustness of the program, achieve very good compatibility when changing, improve the maintainability and scalability of the program, and reduce the risk introduced when changing requirements.
Composite Reuse Principle (CRP), also known as composition / Aggregate Reuse Principle (carp): when reusing software, we should try to implement it by using association relations such as composition or aggregation first, and then consider using inheritance relations
If you want to use inheritance, you must strictly follow the Richter substitution principle. The synthesis Reuse Principle and the Richter substitution principle complement each other, and both are the specific implementation specifications of the opening and closing principle.
It maintains the encapsulation of classes. Because the internal details of the component object are invisible to the new object, this reuse is also called “black box” reuse.
The coupling between old and new classes is low. This reuse requires less dependence. The only way for new objects to access component objects is through the interface of component objects.
High flexibility of reuse. This reuse can be carried out dynamically at runtime, and new objects can dynamically reference objects of the same type as component objects.
The purpose of these principles is only one: reduce the coupling between objects and increase the reusability, scalability and maintainability of programs.
Memory formula: access plus restrictions, functions should be frugal, dependencies are not allowed, interfaces should be added dynamically, parent classes should be abstract, and extensions should not be changed.