我读了很多定义,但仍然不明白 - 外部耦合
和常见耦合
之间的真正区别是什么?示例,从这里:
外部耦合当两个模块共享外部强加的数据格式,通信协议或设备接口时,就会出现。
常见耦合:如果两个模块通过某些全局数据项共享信息,则将耦合两个模块。
据我了解,如果类A
和类B
都使用一些外部 .txt文件
本情况相应外部耦合
和常见耦合
。有人可以举一个例子吗?
I've read a lot of definitions, but still don't understand - what is the real difference between External Coupling
and Common Coupling
? Example, definitions from here:
External Coupling arises when two modules share an externally imposed data format, communication protocols, or device interface.
Common Coupling: Two modules are common coupled if they share information through some global data items.
As I understand, if Class A
and Class B
both use some outer .txt file
this case corresponds External Coupling
and Common Coupling
. Could someone gives a clear example ?
发布评论
评论(1)
您引用的外部耦合的定义如今很普遍,但我不知道它来自何处。据我所知,最初定义了不同类型的耦合。这些概念是针对Cobol和Fortran等语言的,有时很难掌握它们,因为它们指的是非常不同或不存在现代语言的功能。
根据 Myers的定义,外部和共同耦合之间没有太大的区别,这似乎只是指单个全局变量vs. vs.与整个共享的问题包含多个数据的范围:“外部耦合类似于常见耦合,除非在外部耦合中,模块之间的引用是对单个数据项而不是对数据结构的参考”。
当前的定义外部耦合的当前定义不是指全局范围,而是指应用程序本身外部的事物:数据库,硬件,外部库等。如我所见,从这个意义上讲,您的
.txt
文件将是外部耦合。假设您的应用程序的许多类读取.txt
文件。所有这些类都与该特定文件格式相连。如果由于某种原因需要更改文件类型,则需要在代码的不同部分中修复内容。缓解这种形式的耦合的方式是尽可能减少应用程序与外部世界的接触站点。也许您只能有一个类
c
读取.txt
文件,而其他类a
和b
则消耗输出那个阅读。如果需要更改源格式,则只能更改c
的实现,a
和b
可以像以前一样继续消费其输出(只要c
维护其接口)。您可以通过研究适配器模式和 hexagonal架构。The definition of external coupling you are quoting is quite common nowadays, but I don't know where it comes from. As far as I know, the different types of coupling were originally defined by Glen Myers in this book. These concepts were conceived for languages such as Cobol and Fortran, sometimes it is hard to grasp them because they refer to features that are very different or don't exist in modern-day languages.
According to Myers' definition, there is not much difference between external and common coupling, it seems to be just a matter of referring to individual global variables vs. to a whole shared scope containing multiple data: "External coupling is similar to common coupling except that, in external coupling, the references between modules are to individual data items, not to data structures".
Current definitions of external coupling refer not to a global scope but to things that are external to the application itself: databases, hardware, external libraries, etc. As I see it, your example of the
.txt
file would be external coupling in this sense. Suppose that many classes of your application read.txt
files. All those classes are coupled to that specific file format. If for some reason you need to change the type of file, you will need to fix things in different parts of your code.The way of mitigating this form of coupling is to reduce as much as possible the sites of contact of your app with the external world. Perhaps you can have only one class
C
read.txt
files, and other classesA
andB
consume the output of that reading. If you need to change the source format, you can change the implementation ofC
only, andA
andB
can keep consuming its output as before (as long asC
maintains its interface). You can find out more about this by researching the Adapter Pattern and Hexagonal Architecture.