A preliminary understanding of java design principles

Analysis of java design principles:
1. Open and closed principle: (for new development, closed for modification)
             When designing a method, others have already used it, but when this method needs to be modified at this time, in order not to affect other methods that use it, A similar method needs to be added. Make changes on the new method instead of directly modifying the current error.
2. Li-style substitution principle: the embodiment of the relationship between parent and child class, the object of the child class can be directly assigned to the variable of the parent class.
3. Dependency inversion principle: Similar to the Li-style substitution principle, when a variable is required, it is not necessary to know what type the variable is. When a subclass is required, only its parent class needs to be passed in.
4. Interface isolation principle: each class has a clear purpose to do, and the responsibilities of each class cannot be repeated.
5. Synthesis/aggregation reuse principle: Try not to use inheritance where inheritance is not required, and use combination relationships instead. In other words, try to use composition and aggregation instead of inheritance for reuse.
                     Because during inheritance, when the parent class is modified in one place, then the subclass will be modified all. Use a combination of classes as much as possible to use a certain function.
6. Demeter's Law: Also known as the principle of least knowledge, when the code needs to be modified, the affected code is minimized. Make the relationship between classes less complicated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326471660&siteId=291194637