Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the principles of object-oriented design?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the principles of object-oriented design". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn what the principles of object-oriented design are.

Design principles are the theoretical basis of design patterns, and design patterns are their practice. The seven design principles are briefly summarized as shown in the following table:

The design principle generalizes the purpose, the opening and closing principle is open to the extension, the modification is closed, the new risk caused by maintenance is reduced, the Richter replacement principle should not destroy the inheritance system, and the function of the subclass rewriting method should not be changed. it should not affect the meaning of the parent method to prevent the inheritance from flooding, the inversion principle, the high level should not rely on the lower level, and the interface programming should be more conducive to the upgrade and expansion of the code structure. the principle of single responsibility does only one thing. The implementation class should be single and easy to understand, improve the readability of the code, the interface isolation principle only does one thing, the interface should simplify the single function decoupling, do not know what the high aggregation and low coupling Dimitt principle should not know, a class should maintain the least understanding of other objects, reduce the degree of coupling, reduce code bloated synthesis reuse principle, try to use combination or aggregation relationship to achieve code reuse. Reduce the use of inheritance to reduce code coupling 1. Principle of opening and closing

The idea of principle: software entities should be open to extension and close to modification.

Description: a software product will change during its life cycle, since change is an established fact, when the requirements of the application change, without modifying the source code or binary code of the software entity, the function of the module can be extended to meet new requirements.

Advantages: improve the reusability of the code, improve the scalability and maintainability of the software.

2. Richter substitution principle

Idea: inheritance must ensure that the properties owned by the superclass still exist in the subclass.

Description: subclasses can extend the functionality of the parent class, but cannot change the original functionality of the parent class. The subclass can implement the abstract method of the parent class, but it cannot override the non-abstract method of the parent class, and the subclass can add its own unique methods. It reflects the relationship between the base class and the subclass, complements the opening and closing principle, and standardizes the concrete steps to realize abstraction.

Advantages: increase the robustness of the program, class expansion will not introduce new errors to the existing system, reducing the possibility of code errors.

3. The principle of dependency inversion

Idea: high-level modules should not rely on low-level modules, they should all rely on abstraction, abstraction should not depend on concrete implementation, concrete implementation should depend on abstraction.

Description: in software design, the details are variable, while the abstraction layer is relatively stable, so the architecture based on abstraction is much more stable than the architecture based on details. The purpose of using interfaces or abstract classes is to develop specifications and contracts without involving any specific operations and leave the task of presenting details to their implementation classes. The dependency inversion principle is one of the important ways to realize the opening and closing principle, which reduces the coupling between the customer and the implementation module.

Advantages: reduce the coupling between classes, improve the readability and maintainability of the code, and reduce the risks caused by parallel development.

4. Principle of single responsibility

Idea: a class (or method www.jqmms.com) is only responsible for one thing. It is to control the granularity of the class, decouple the object and improve its cohesion.

Description: also known as the single function principle, the responsibility here refers to the cause of the class change, the single responsibility principle states that a class should have and only one cause for its change, otherwise the class should be split. If an object takes on too many responsibilities, there are at least two shortcomings: 1, a change in responsibility may weaken or restrain the class's ability to perform other responsibilities; 2, when the client needs a responsibility of the object, have to include all other unnecessary responsibilities, resulting in redundant code or code waste.

Advantages: reduce the coupling of class and class, reduce the complexity of class, improve readability, increase maintainability and extensibility, and reduce the risk of variability.

5. The principle of interface isolation

The idea of principle: classes and classes should be based on the minimum interface.

Description: programmers are required to split bloated interfaces into smaller and more specific interfaces as much as possible, so that they contain only methods of interest to customers. Instead of trying to create a large interface for all classes that depend on it, create a dedicated interface for each class that they need.

Advantages: improve program flexibility, improve cohesion, reduce external interaction, and make the smallest interface do the most things.

Note: the granularity of interfaces needs to be reasonably defined. If the definition is too small, it will result in too many interfaces and complicate the design; if the definition is too large and the flexibility is reduced, it is unable to provide customized services, which brings unforeseen risks to the overall project.

The principle of interface isolation and single responsibility are both designed to improve the cohesion of classes and reduce the coupling between them, reflecting the idea of encapsulation, but the two are different:

# the single responsibility principle focuses on responsibility, while the interface isolation principle focuses on the isolation of interface dependencies.

# the principle of single responsibility is mainly about constraint classes, which aims at the implementation and details of the program; the principle of interface isolation mainly constrains interfaces, mainly for abstraction and the construction of the overall framework of the program.

6. Dimitt principle (least known)

The idea of principle: an object should know as little as possible about other objects, namely, decoupling between classes.

Description: a class minimizes its dependence on other objects, and the principle is low coupling and high cohesion. Only by keeping the coupling between modules as low as possible can the code reuse rate be improved.

Advantages: low coupling, high cohesion, improve the reusability of classes and the scalability of the system.

Note: excessive use of Demeter rule will cause the system to produce a large number of intermediary classes, which will increase the complexity of the system and reduce the efficiency of communication between modules. Therefore, the Demeter rule needs to be weighed repeatedly to ensure high cohesion and low coupling, as well as a clear structure of the system.

7. The principle of synthetic reuse

Idea of principle: because composition or aggregation can incorporate existing objects into new objects and make them part of new objects, new objects can invoke the functions of existing objects.

Description: it requires that in software reuse, association relationships such as composition or aggregation should be used as far as possible, and then inheritance relationships should be considered. In general, if a role gets more responsibility, you can use a composite / aggregation relationship to delegate new responsibilities to the appropriate object. Of course, this reuse also has its drawbacks. The main disadvantage is that the system built through this reuse will have more objects to manage.

Advantages: it maintains the encapsulation of the class, and the internal details of the component object are invisible to the new object, so this kind of reuse is also called "black box" reuse. The coupling between new and old classes is low, and this kind of reuse requires less dependency. the only way for new objects to access component objects is through the interface of component objects. The flexibility of reuse is high, this kind of reuse can be carried out dynamically at run time, and new objects can dynamically refer to objects of the same type as component objects.

Summary

In fact, these principles have only one purpose: to reduce the coupling between objects and to increase the reusability, extensibility and maintainability of the program. In the actual development process, it does not necessarily require all the code to follow the design principles, but to comprehensively consider the manpower, time, cost and quality, do not deliberately pursue perfection, but follow the design principles in the appropriate scene. This reflects a balanced trade-off that can help us design a more elegant code structure.

Thank you for your reading. the above is the content of "what are the principles of object-oriented design". After the study of this article, I believe you have a deeper understanding of what the principles of object-oriented design are. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report