In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what are the basic principles of java design pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Design patterns (Design pattern) represent best practices in program development and are often adopted by experienced object-oriented software developers. Design pattern is the solution to the general problems faced by software developers in the process of software development. These solutions are summed up by many software developers after a long period of trial and error. What are the basic design principles of design patterns?
Generally speaking, the basic principles of design patterns include the following seven elements:
Single responsibility principle (Single Responsibility Principle)
The principle of single responsibility represents the functional correlation between the constituent elements of a module. From the perspective of software change, as far as a class is concerned, there should be only one reason for it to change; in popular terms, a class is responsible for only one responsibility.
SRP is a simple and intuitive principle, but it is difficult to use it properly in the actual coding process, so it needs to be used in combination with the actual situation.
The principle of single responsibility can reduce the complexity of a class. A class is responsible for only one responsibility, and its logic is certainly simpler than that of multiple responsibilities.
It improves the readability of the code and the maintainability of the system.
Open-close principle (Open-Closed Principle)
The open-close principle means that software entities (classes, modules, functions, etc.) should be extensible, but not modifiable.
If a software can meet the OCP principle, it will have two advantages:
It can expand the existing system and provide new functions to meet new requirements, so the software has strong adaptability and flexibility.
Existing modules, especially those important abstract modules, do not need to be modified, so the software has strong stability and persistence.
Richter substitution principle (Liskov Substitution Principle)
Replace a base class object with its subclass object, the program will not produce any errors and exceptions, and vice versa, if a software entity uses a subclass object, then it may not be able to use the base class object. That is, the subclass can extend the function of the parent class, but can not change the original function of the parent class.
Dependency inversion principle (Dependence Inversion Principle)
High-level modules should not rely on lower-level modules, both should be abstract. Furthermore, abstraction should not depend on details, which should depend on abstractions. Following the principle of dependency inversion can reduce the coupling between classes, improve the stability of the system, and reduce the risk caused by modifying the program. The core of relying on the inversion principle is that we should program to the interface.
Interface isolation principle (Interface Segregation Principle)
The client should not rely on interfaces it does not need; the dependence of one class on another should be based on the smallest interface.
The idea of interface isolation principle is to establish a single interface, to refine the interface as much as possible, and to have as few methods in the interface as possible.
But there must be a limit to everything. If the interface design is too small, it will cause too many interfaces and complicate the design. So it must be moderate.
Dimitt's rule (Law Of Demeter)
The Dimitt rule, also known as the least knowing principle, means that an object should have the least understanding of other objects. In popular terms, only communicate with direct friends.
Direct friends: each object will have a coupling relationship with other objects, as long as there is a coupling relationship between two objects, we will say that the two objects are friends. There are many ways of coupling, such as dependence, association, combination, aggregation and so on. Among them, we call the classes in the member variables, method parameters and method return values as direct friends, while the classes that appear in local variables are not direct friends. In other words, a strange class had better not appear inside the class as a local variable.
For dependent classes, no matter how complex the logic is, try to encapsulate the logic inside the class, provide public methods to the outside, and do not disclose any information.
Combination / aggregation reuse principle (Composite/Aggregate Reuse Principle)
The principle of composition / aggregation reuse is to use some existing objects in a new object to make it part of the new object; the new object achieves the purpose of reusing the existing function by delegating to these objects.
In object-oriented design, if you inherit the base class directly, it will break the encapsulation, because inheritance exposes the implementation details of the base class to the subclass; if the implementation of the base class changes, the implementation of the subclass has to change; the implementation inherited from the base class is static, impossible to change at run time, and does not have enough flexibility. Therefore, the principle of composition / aggregation reuse is put forward, that is, in the actual development and design, composition / aggregation should be used as much as possible, rather than class inheritance.
Generally speaking, the principle of composition / aggregation reuse tells us that composition or aggregation is better than inheritance.
Aggregation composition is a kind of "black box" reuse because the content of the detail object is invisible to the client.
This is the end of the content of "what are the basic principles of the java design pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.