Coupling and decoupling

1. Coupling

1. Coupling refers to the phenomenon that two or more systems or two forms of motion influence each other and even unite through interaction.

2. In software engineering, the degree of coupling between objects is the dependence between objects. The higher the coupling between objects, the higher the maintenance cost. Therefore, the design of objects should minimize the coupling between classes and components.

3. Classification: There are coupling between software and hardware, as well as coupling between software modules. Coupling is a measure of the correlation between the various modules in the program structure. It depends on the complexity of the interface between the various modules, the way the modules are called, and what information passes through the interface.

Two, decoupling

1. Decoupling, literally means decoupling.

2. In software engineering, reducing the degree of coupling can be understood as decoupling. If there are dependencies between modules, there must be coupling. In theory, absolute zero coupling cannot be achieved, but the degree of coupling can be reduced by some existing methods. To the lowest.

3. The core idea of ​​design: reduce code coupling as much as possible. If code coupling is found, decoupling technology must be adopted. Let the data model, business logic and view display reduce the coupling between the three layers, and minimize the correlation dependence, so as not to affect the whole body. The principle is that the code of function A should not be written in the function code of B. If there is a need for interaction between the two, you can use interfaces, messages, or even introduce frameworks, but in short, don't cross-write directly.

4. Observer mode: The meaning of the observer mode is "decoupling", which makes the logic of the observer and the observed no longer be mixed together, but independent and independent of each other. For example, the night mode of NetEase News. When the user switches to the night mode, the observer will notify all observers that "the settings have changed, so let's cover it soon." After the message is pushed, it is necessary to pop a push on the notification bar and mark a small red dot on the desktop, which is also a clever cooperation between the observer and the observed.

Guess you like

Origin blog.csdn.net/guodashen007/article/details/103251592