Example analysis of high cohesion and low coupling rule

Definition: an object should have the least understanding of other objects.

The origin of the problem: the relationship between classes is getting closer and closer, and the coupling degree is getting larger and larger. When one class changes, it will have a greater impact on another class

Solution: minimize the coupling between classes

Since we came into contact with programming, we have known the general principle of software design, low coupling and high cohesion. Whether it is object-oriented or process-oriented, the coupling degree should be as low as possible in order to improve the reuse rate of code. But how to program low coupling?

No matter how complex the logic is, for dependent classes, try to encapsulate the logic inside the class, and do not disclose any information except the public method provided. There is a simpler definition: only communicate with direct friends. First, explain what is a direct friend; Each object will be coupled with other objects. We say that there is a coupling relationship between the two objects. We say that the two objects have a friend relationship. There are many ways of coupling, such as dependency, association, combination, aggregation and so on. Among them, we call the classes with member variables, method parameters and method return values as direct friends, while the classes with local variables are not direct friends, that is, strange classes should not appear inside the class as local variables;

For example, in a school, there are several teachers, numbered in turn. There are several students below, numbered once. Now it is required to print out the ID of all teachers and students

First, the principle of low coupling and high cohesion is violated

The code is as follows.

Now the main problem of this design appears in the teachermanage class. According to the law of low coupling and high cohesion, it only communicates with direct friends, while the student class is not a direct friend in the teachermanage class. Such coupling of indirect friends should be avoided in the class.

The modified code is as follows:

After modification, the student adds a new method of student ID, which can be called directly by the teacher. So as to avoid coupling with students. The original intention of the principle of low coupling and high cohesion is to reduce the coupling between classes. Because each class reduces unnecessary dependencies, it can indeed reduce the coupling relationship, but everything has a degree. Although it can avoid communicating with indirect classes, to communicate, the relationship must occur through an "intermediary". This rule can achieve clear structure, high cohesion, low coupling

Coupling and cohesion are two qualitative criteria for module independence. When dividing the software system into modules, try to achieve high cohesion and low coupling, improve the independence of modules, and lay the foundation for the design of high-quality software structure.

An example is easy to understand: a program has 50 functions, and the program performs very well; However, once you modify one of the functions, the other 49 functions need to be modified, which is the consequence of high coupling.

summary

The above is all about the code analysis of the example of the law of high cohesion and low coupling in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>