In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of what the six principles of design patterns are, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the six principles of design patterns. Let's take a look.
Principle of single responsibility
Single responsibility principle (Single Responsibility Principle)
There should never be more than one reason for a class to change. (there is one and only one reason to change the class)
No matter what you ask me to do, I only do one thing. You ask me to go downstairs to pick up the express delivery and take the garbage down by the way. I'm sorry, I don't do it, I only take the express delivery.
Advantages:
The complexity of the class is reduced, and all responsibilities are clearly defined; the readability is improved and the complexity is reduced, of course, the readability is improved; the maintainability is improved, and the readability is improved, of course, it is easier to maintain. Change is essential to reduce the risk caused by change. if the single responsibility of the interface is done well, one interface modification only affects the corresponding implementation class, but not the other interfaces. This is of great help to the scalability and maintainability of the system. Interface isolation principle
Interface isolation principle (Interface Segregation Principle)
1. Clients should not be forced to depend upon interfaces that they don't use. (the client should not rely on interfaces it does not need)
2. The dependency of one class to another one should depend on the smallest possible interface. (dependencies between classes should be based on the smallest interface)
For example, class A depends on class B, method 1, method 2, method 3 through Interface1; class B depends on D, method 1, method 4, and method 5 through Interface1 to see what happens when the interface isolation principle is not used and the interface isolation principle is used.
Principle of dependency inversion
Dependency inversion principle (Dependence Inversion Principle)
1 、 High level modules should not depend upon low level modules.Both should depend upon abstractions. (high-level modules should not rely on lower-level modules, both should rely on their abstraction)
2 、 Abstractions should not depend upon details. (abstractions should not rely on details)
3 、 Details should depend upon abstractions. (details should rely on abstraction)
For example, class A directly depends on class B. if you want to change class A to dependency class C, you must modify the code of class A. In this scenario, class An is generally a high-level module responsible for complex business logic; class B and class C are low-level modules responsible for basic atomic operations; if class An is modified, it will bring unnecessary risks to the program.
Solution: modify class A to rely on interface Interface1, class B and class C respectively implement interface Interface1, and class An indirectly contacts class B or class C through interface Interface1, which will greatly reduce the probability of modifying class A.
Richter's substitution principle
Richter substitution principle (Liskov Substitution Principle)
1. If for each object o1 of type S there is an object O2 of type T such that for all programs P defined in terms of T behavior of P is unchanged when o1 is substituted for O2 then S is a subtype of T. (if for every object o1 of type S, there is an object o2 of type T, so that when all program P defined by T is replaced by o2, the behavior of program P does not change, then type S is a subtype of type T.)
2. Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. (all references to the base class must be able to transparently use the objects of its subclasses)
For example, there is a function P1, which is completed by class A, and now it is necessary to extend function P1. The expanded function is P, where P consists of the original function P1 and the new function P2, and the new function P is completed by the subclass B of class A. while completing the new function P2, subclass B may cause the failure of the original function P1.
Solution: follow the Richter substitution principle when using inheritance. When class B inherits class A, in addition to adding new methods to complete the new function P2, try not to overwrite the methods of parent class A, and try not to overload the methods of parent class A.
Inheritance contains such a meaning: all the implemented methods in the parent class are actually setting a series of specifications and contracts, although it does not force all subclasses to comply with these contracts. however, if the subclass modifies these methods arbitrarily, it will destroy the whole inheritance system, and the Richter substitution principle expresses this meaning.
As one of the three characteristics of object-oriented, inheritance not only brings great convenience to programming, but also brings disadvantages. For example, the use of inheritance will bring intrusiveness to the program, reduce the portability of the program, and increase the coupling between objects. if a class is inherited by other classes, all subclasses must be taken into account when the class needs to be modified. and after the parent class is modified, all functions related to the subclass may fail.
Advantages:
Code sharing reduces the workload of creating classes, each subclass has the methods and properties of the parent class, and improves the reusability and expansibility of the code. Improve the openness of the product or project. Demeter's rule
Dimitt's rule (Law of Demeter)
1. Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. (each unit can only have limited knowledge about other units: only units that are closely related to the current unit)
2. Each unit should only talk to its friends; don't talk to strangers. (each unit can only talk with its friends: not with strange units.)
3. Only talk to your immediate friends. (only talk to your direct friends)
For example, we read Wechat and read books in APP through mobile phone. How to write the design class?
Mobile phone class and book class, these two can not have a direct call relationship, need to have a call relationship between the mobile phone class and Wechat Reader APP class first, and then Wechat Reading APP class and book class can have a call relationship, in order to follow the Dimitt rule.
Opening and closing principle
Opening and closing principle (Open Closed Principle)
Software entities (classes, modules, functions, etc.) Should be open for extension, but closed for modification. (objects (classes, modules, functions, etc.) in software should be open for extensions, but closed for modifications.
During the life cycle of the software, when the original code of the software needs to be modified due to changes, upgrades, maintenance and other reasons, errors may be introduced into the old code, or we may have to ReFactor the whole function. and the original code needs to be retested.
Solution: when the software needs to change, try to achieve the change by extending the behavior of the software entity, rather than by modifying the existing code.
This is the end of the article on "what are the six principles of design patterns". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the six principles of design patterns". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.