For loose coupling and interface

xiaoxiao2021-03-06  45

Application of loose coupling and interface

1 Introduction

1.1 summary

A software, it has many classes, and the class and classes need to be called each other. Once a class is closely coupled with another class, the reuse of this software will greatly decrease. Therefore, the high low of the reuse of a software depends on its high degree of coupling.

1.2 noun explanation

1.2.1 Coupling: The relationship between program modules, dependence.

1.3 keyword

Low coupling; interface design; module reuse;

2 question

2.1 Proposed during the design process

When designing this software architecture, it is discovered that the logical operation section of this software (Simplerouter Class) and the output section cannot be combined very well. That is, we have to pass the reference to the program interface to the core part of the program to provide output function.

2.2 Proposed during the development process

When we have modified the output interface (SimpleterFrame class), especially some method name modifications, the code of the corresponding program core part needs to be re-modified to accommodate this new output interface.

3 reasons for questions

The coupling between classes and classes is too close, so that each time you need to modify a class, it needs to modify the code to accommodate this modified class.

For example: a class A requires a PUBLIC method that calls another class B, once b no longer supports this method, or rewritten this method name, A will need to rewrite the code to adapt. Alternative: A class A needs to use class B with a particular method, but the form of B is uncertain. Once the internal structure of B changes, A may need to rewrite the code.

4 solution solution

To avoid this, there is a need to reduce the coupling between A and B, and the purpose is that regardless of the form of B, as long as B is still able to implement A required function, A does not need to rewrite the code.

Workaround: Let B implement some interface i, define I.Method (); simultaneous A method of calling i directly when calling B method; B method of the method:

{

A.amethod (b) {

B.BMETHOD ();

/*....

}

}

changed to:

{

A.amethod (i) {

I.Method ();

}

}

Here, b only needs to implement the I.Method () method, completely hidden the implementation details.

According to this approach, there is only a loose coupling between classes and classes, which greatly enhances the reusability of classes. Review the previously learned design patterns, you can find that this is similar to the Observer mode.

转载请注明原文地址:https://www.9cbs.com/read-79070.html

New Post(0)