search
HomeJavaJavagetting StartedUnderstanding the concepts of high cohesion and low coupling

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.