外部耦合与常见耦合

发布于 2025-01-20 20:14:59 字数 521 浏览 2 评论 0 原文

我读了很多定义,但仍然不明白 - 外部耦合常见耦合之间的真正区别是什么?示例,从这里

外部耦合当两个模块共享外部强加的数据格式,通信协议或设备接口时,就会出现。

常见耦合:如果两个模块通过某些全局数据项共享信息,则将耦合两个模块。

据我了解,如果类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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你搞怪i 2025-01-27 20:14:59

您引用的外部耦合的定义如今很普遍,但我不知道它来自何处。据我所知,最初定义了不同类型的耦合。这些概念是针对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 classes A and B consume the output of that reading. If you need to change the source format, you can change the implementation of C only, and A and B can keep consuming its output as before (as long as C maintains its interface). You can find out more about this by researching the Adapter Pattern and Hexagonal Architecture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文