How to Implement High Cohesion coupling in programming

Source: Internet
Author: User

Cohesion: the closeness of each element in a module.

Coupling: a measurement of the degree of interconnection between different modules in a software architecture

We are always pursuing high cohesion and low coupling.

Low coupling:

A complete system, between modules, try to make it independent.

That is to say, let each module complete a specific sub-function as independently as possible.

The interfaces between modules are as few as possible and as simple as possible.

If the relationship between two modules is complex, it is best to consider further module division first.

This facilitates modification and combination.

Low coupling:

In a module, each element is connected as closely as possible.

That is to say, we make full use of the functions of each element to implement a function.

If the relationship between an element and the module is loose, the structure of the module may be incomplete, or the element is redundant.

Cohesion and coupling, including the relationship between horizontal and vertical. Function cohesion and data coupling are what we need to achieve. Horizontal cohesion and coupling are usually reflected in the relationship between modules and classes of the system. Vertical Coupling is reflected in the relationship between various levels of the system.

For my confusion in coding, I think like this, using the object-oriented idea to consider the encapsulation of a class.

One method, how to encapsulate it, can be seen in real life to see if this ability (method) belongs to this kind of thing (class) instinct.

If yes, It is encapsulated in this class.

If not, consider encapsulating it in other classes.

If such capabilities are available to many things, they must be encapsulated in the general category of such things.

If this capability is frequently used by many things, it can be encapsulated into a static method of the general class.

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 reflectsProgramPersonnel 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. Highly cohesive modules have less dependence on each other. When a module encounters a problem, it can control errors in one module. it does not affect other functional modules because of a module failure. this guarantees the integrity of other functions, does not affect other operations, and minimizes the user's troubles. programmers can quickly locate the module where the problem is located and modify the problem. there is not much involvement between modules, making it easier for programmers to modify problems. after the problem is modified, it will not affect other functional modules too much. when performing unit or integration testing, testers can easily clarify the logic between program modules and design high-coverage use cases to programCodeAfter the module interface can completely cover the test. It can also reduce the risk when testing regression.

Modules with high coupling are highly dependent on each other, and the relationship between modules is also complicated. it is easy to see that a program module has a problem, resulting in failure of other functional modules. the complexity of the relationship makes it inconvenient for programmers to locate and modify problems. it also affects all operations of users and causes greater losses to users. in addition, due to the complex relationship between modules, even after the programmer modifies a problem, it cannot be guaranteed that it will not bring more defects. it makes too much inconvenience for testers to perform unit and integration tests. it takes a lot of time to sort out the internal logic. in this case, the test may also be omitted, leaving hidden risks. testers also need to spend more time performing regression tests to ensure that the system program meets the required requirements. it brings more risks to the project.

Therefore, when writing a large system, try to improve the cohesion of the program and reduce the coupling program of the program. Tight coupling means that functions are complex and many parameters are passed. Functions with complex functions are decomposed into independent modules for High Cohesion and loose coupling.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.