Home  >  Article  >  Java  >  Understanding the concepts of high cohesion and low coupling

Understanding the concepts of high cohesion and low coupling

王林
王林Original
2020-06-16 15:47:124062browse

Understanding the concepts of high cohesion and low coupling

Concept

High cohesion and low coupling is a concept in software engineering. It is a standard for judging the quality of software design. It is mainly used for The object-oriented design of a program mainly depends on whether the cohesion of the class is high and the degree of coupling is low.

Purpose

To greatly enhance the reusability and portability of program modules.

Generally, the higher the degree of cohesion of each module in the program structure, the lower the degree of coupling between modules. Cohesion measures the connections within a module from a functional perspective. A good cohesive module should do exactly one thing, and it describes the functional connections within the module; coupling is a type of interconnection between modules in the software structure. Metrically, the strength of coupling depends on the complexity of the interface between modules, the point at which a module is entered or accessed, and the data passing through the interface.

(Video tutorial recommendation: java video tutorial)

Methods to reduce coupling

1. Use less classes Inheritance and multi-purpose interfaces hide implementation details. In addition to supporting polymorphism, Java object-oriented programming introduces interfaces to hide implementation details.

2. The functional division of modules should be as simple as possible. The reason is also very simple. Modules with single functions have fewer opportunities for other modules to call them. (In fact, this is a way of saying high cohesion. High cohesion and low coupling generally appear at the same time).

3. Follow a definition and only appear in one place.

4. Use global variables less.

5. Use less public and more private keywords when declaring class attributes and methods.

6. Multi-purpose design patterns. For example, using the MVC design pattern can reduce the coupling between the interface and business logic.

7. Try not to write programs in a "hard-coded" way, and try to avoid directly using SQL statements to operate the database.

8. Finally, of course, avoid directly operating or calling other modules or classes (content coupling); if coupling must exist between modules, in principle try to use data coupling, use less control coupling, and limit the scope of public coupling. Avoid using content coupling.

Methods to enhance cohesion

1. The module only exposes the minimum interface to the outside world, forming the lowest dependency.

2. As long as the external interface remains unchanged, modifications within the module must not affect other modules.

3. Deleting a module should only affect other modules that have dependencies, and should not affect other irrelevant parts.

Recommended tutorial: java entry program

The above is the detailed content of Understanding the concepts of high cohesion and low coupling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn