High cohesion, low coupling


What is high cohesion and low coupling?

    Cause: Module independence means that each module only completes the independent sub-functions required by the system, and has the least connection with other modules and a simple interface. Two qualitative metrics—coupling and cohesion.

    Coupling is also called inter-block connection. Refers to a measure of the degree of interconnection between modules in a software system structure. The closer the connection between the modules, the stronger the coupling, and the worse the independence of the modules. The level of coupling between modules depends on the complexity of the interface between modules, the way of calling and the information passed.
    Coupling classification (low-high): no direct coupling; data coupling; tag coupling; control coupling; public coupling; content coupling;
1 Without direct coupling:
2 Data coupling: refers to the calling relationship between the two modules, and the transmission is a simple data value, which is equivalent to the value transmission in a high-level language;
3 Tag coupling: Refers to the data structure passed between the two modules, such as the array name, record name, file name and other names in the high-level language are the tags, in fact, the address of the data structure is passed;
4 Control coupling: When a module calls another module, it passes a control variable (such as a switch, a flag, etc.), and the called module selectively executes a function in the block through the value of the control variable;
5 Common coupling: refers to the coupling between those modules that interact through a common data environment. The complex program of public coupling increases as the number of coupled modules increases.
6 Content coupling: This is the highest degree of coupling and the worst. When a module directly uses the internal data of another module, or transfers it into another module through abnormal entry.
 
    Cohesion is also known as intra-block linkage. Refers to a measure of the functional strength of a module, that is, a measure of the closeness of each element within a module combined with each other. If the elements in a module (between language names and between program segments) are more closely related, the higher its cohesion is.
    Cohesive Bandits (Low-High): Accidental Cohesion; Logical Cohesion; Temporal Cohesion; Communication Cohesion; Sequential Cohesion; Functional Cohesion;
1 Accidental cohesion: refers to the absence of any connection between processing elements within a module.
2 Logical cohesion: Refers to the execution of several logically similar functions within a module, and which function the module completes is determined by parameters.
3 Time aggregation: The module formed by combining the actions that need to be executed at the same time is the time aggregation module.
4 Communication cohesion: It means that all processing elements in a module operate on the same data structure (sometimes called information cohesion), or that each process uses the same input data or produces the same output data.
5 Sequential cohesion: It means that each processing element in a module is closely related to the same function and must be executed sequentially, and the output of the previous functional element is the input of the next functional element.
6 Functional cohesion: This is the strongest cohesion, which means that all elements in the module work together to complete a function, which is indispensable. Coupling with other modules is the weakest.
 
    Coupling and cohesion are two qualitative criteria for module independence. When dividing a software system into modules, try to achieve high cohesion and low coupling, improve the independence of modules, and lay the foundation for designing high-quality software structures.
 
    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.
Once you understand it, it's natural to design classes or modules with "high cohesion, low coupling" in mind when you write high-level designs.




Guess you like

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