The IT industry has a famous slogan: strong cohesion and loose coupling.
Even the most elementary programmer, who is often taught, understands the meaning of this slogan: Our program should be modularized, the module must complete a clear set of associated service functions, and each part of the module must be related and organically combined into a complete body (external programs look at the Black Box ), the components in the module should be highly correlated (strong cohesion) as much as possible, while the modules and modules should be split and less dependent (loosely coupled ).
It is easy for people to implement strong cohesion modules. For example, a function implements an independent function, which is a strong cohesion.
It is difficult for people to achieve loose coupling, Because lonely modules are meaningless. Only by working in a coordinated manner between modules can we achieve the purpose of the system. It is hard to grasp the relationship between modules without some experience. The coupling strength depends on: (1) calls from one module to another; (2) data volume transferred from one module to another; (3) the amount of control that a module applies to another module; (4) the complexity of interfaces between modules. And so on.
Of course, "strong cohesion and loose coupling" also have contradictions. For example, the stronger the cohesion, the more functions are required (each function only performs one "thing, combining them into "big" functions makes it more complicated and cannot achieve loose coupling. Therefore, we should make a balance and compromise between the two, which also reflects the programmer's level. From the perspective of system theory, the system is hierarchical, that is, the system can be divided into subsystems, modules can be divided into sub-modules, and the "degree" of "strong cohesion and loose coupling" can be grasped, it should be considered in combination with the sub-layer of the system, that is, the compromise should be made at the level. For example, the subprograms in the module (at the next level) should share data (with a certain degree of Coupling Degree ), reducing global variables reduces the coupling between subprograms.
The object-oriented language further strengthens "strong cohesion and loose coupling". The encapsulation of classes emphasizes the cohesion of related content (data and operations, it also emphasizes the independence and privacy of classes. The inheritance of classes and the relationship between classes are standardized under the principle of loose coupling. Classes and Classes usually implement the service provider/service requestor mode through the interface contract, which is a typical loose coupling.
"Strong cohesion and loose coupling" are important for division of labor in programming, maintainability of programs, and testing. For example, from the design perspective, the program module designed under the guidance of "strong cohesion and loose coupling" meets the WBS (work breakdown structure) requirements of project management, its relatively independent modules can be allocated to specific programmers for development. In addition, program coding outsourcing must also be built under the design of this principle. From the perspective of program life cycle, it is conducive to improving the quality of the program, especially for future maintenance of the program, that is, the relative independence of the program module is the guarantee of maintainability. From the perspective of testing, in line with the "strong cohesion, loose coupling" program, it is easy to perform black box testing on local (module), and also easy to compile the "pile" and "Drive" for testing ".
"Strong cohesion and loose coupling" is also a requirement on the organizational structure. The project team is divided into several groups (formal or informal). The work of each group should be highly relevant, the work between groups should be less relevant or have clear interfaces as far as possible to reduce communication costs. In fact, "strong cohesion and loose coupling" is a universal principle in the system. We can find its application in many fields.
"Strong cohesion and loose coupling" is the "Three-character Sutra" we have to read.