Labels: Object-oriented Design Principles
After the previous in-depth explanation, we have basically made it clear about the solid principle. However, if you want to skillfully apply the solid principle, it is not enough to know what the solid is, we also need to know when and when the solid principle is applied (when or where ).
Fortunately, the five independence principles of the solid principle are essentially independent in actual applications, it is not necessary to select an optimal principle from the optional principles at the same time somewhere, which greatly reduces the difficulty of applying the solid principle.
The solid principle is applicable to the following scenarios:
- SRP principle: used for Class Design
When we come up with a class or design a class prototype, use the SRP principle to check whether the class design meets the SRP requirements.
- OCP principle: General Guiding Ideology
The OCP principle is a general guiding ideology. In the object-oriented design, if it can comply with the LSP/ISP/DIP principle, it can generally comply with the OCP principle.
In addition to object-oriented software design, OCP can also be used to guide system architecture design. For example, common CORBA and COM protocols can be considered as the specific application and implementation of the OCP principle.
- LSP principle: used to guide the design of class inheritance
When we design the inheritance relationship between classes, we use the LSP principle to determine whether the inheritance relationship meets the LSP requirements.
- ISP principle: used to guide Interface Design
The ISP principle can be considered as a variant of the SRP principle, which is essentially the same as the SRP principle. SRP is used to guide the design of classes, while ISP is used to guide the design of interfaces.
- Dip principle: used to guide the design of class dependency
When designing the dependency between classes, we can use the dip principle to determine whether the dependency complies with the dip principle.
Dip principles and LSP principles complement each other: dip principles are used to guide the abstraction of interfaces or abstract classes, while LSP principles guide the derivation of New subclasses from interfaces or abstract classes.