Six principles of design patterns
Summary:
Principles |
Description |
Method |
Principle of opening/closing |
Open to extension and disable Modification |
Use abstract classes and interfaces |
Rishi replacement principle |
The base class can be replaced by the quilt class. |
Use abstract class inheritance instead of specific class inheritance |
Dependency reversal Principle |
Must depend on abstraction, not specific |
For interface programming, not for implementation programming |
Interface isolation principle |
Using multiple isolated interfaces is better than using a single interface |
Create minimum Interface |
Dimit Law |
A software entity interacts with other entities as little as possible. |
Establish connections through intermediate classes |
Merging Reuse Principle |
Try to use synthesis/aggregation instead of inheritance. |
Try to use synthesis/aggregation instead of inheritance. |
I. Principle of opening/closing:
In a word: it is open to extensions and closed to modifications.
Why should we follow the principle of opening/closing:
The code developed by the software must undergo multiple tests before it can be used. If the original code is modified, other functions of the software cannot be properly implemented, the code needs to be thoroughly tested again, and some changes need to be made to the original code in a large scale or even re-constructed.
Solution: use abstract classes and interfaces.
Ii. Li's replacement principle
In a word, subclasses must be able to replace them with their base classes.
A more detailed explanation: In a software system, subclass should be able to replace any location where the base class can appear, and after replacement, the code will work normally. subclass can also add new behaviors based on the base class.
Lishi replacement is a supplement to the open and closed principle. He talks about the relationship between the base class and the subclass. Only when such a relationship exists can The Lishi replacement relationship exist.
Example of rectangle and square:
If we look at the left graph, the rectangular area and the square area are obviously different (assuming the square area here is (width + height)/2) ^ 2 ), it does not match the Lee's replacement.
According to the picture on the right.
It can be seen that during the design, we should try to inherit from the abstract class rather than from the specific class. if we look at the inheritance tree, all leaf nodes should be specific classes, and all tree branches should be abstract classes or interfaces. of course, this is just a general guiding principle, and we need to analyze the specific situation when using it.
Iii. Principle of dependency reversal
In a word, we need to rely on abstraction, not on specifics.
Dependency is coupling, which can be divided into three types:
1. Zero-coupling (Nil Couping) relationship: the two classes have no dependency relationship (basically, these two classes do not call each other, or even have no relationship)
2. concrete Couping: two classes are dependent on each other. If one class directly references another class, this relationship exists. (that is, the code we first wrote, directly new objects)
3. abstract Couping relationship: This relationship occurs between a specific class and an Abstract class. This maximizes the flexibility between classes that must have a relationship. (This must be related when calling Service in Action, so we use the interface and use spring factory for automatic injection)
To implement the Dependency inversion principle, the key is to use abstract coupling (where coupling is required, use interfaces ). abstract coupling always involves the inheritance of a specific class from an abstract class, and must be ensured that the class can be changed to other sub-classes at any place referenced to a class. Therefore, the ryth replacement is the basis of the dependency inversion principle, the principle of dependency inversion is the core principle of OOD. The research and application of design patterns are his guiding principles.
Iv. Interface isolation principles
In a word, it is better to use multiple isolated interfaces than to use a single interface.
For example, the left side does not comply with interface isolation, and the right side does:
→
V. dimit's law (least aware principle)
In a word: talk only to your immediate friends.
One class knows less about the interior of other classes. Reduces coupling.
Vi. Principles of merging and reuse
In a word: Use aggregation/combination as much as possible, and use inheritance less.
Inheritance, combination, and aggregation are used for reuse.
When to use inheritance, all the following conditions must be met:
1. Subclass is a special category of superclasses, rather than a role of superclasses, that is, to distinguish between Has-a and is-.
2. There will never be a situation where you need to replace the subclass with another subclass. If you are not sure whether it will become another subclass in the future, do not use inheritance.
3. subclass has the responsibility to expand the superclass, rather than having the responsibility to replace or cancel (Nullify) The superclass. If a subclass needs to replace a large number of superclasses, this class should not be a subclass of this superclass.
4. inheritance can be used only when it is meaningful from the perspective of taxonomy. do not inherit from the tool class.
For details about the design patterns, refer to: Java master Zhenjing series-JavaWeb system design and architecture.