是否将循环依赖项视为不良设计? [英] Are circular dependencies considered bad design?
本文介绍了是否将循环依赖项视为不良设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的工作中(这是90%的Java,但是我确定这个问题也适用于其他语言),我经常创建两个彼此了解"的类.更具体地说,A类导入B,B类导入A类,并且都具有另一种类型的成员或局部变量.
In my work (which is 90% Java but I'm sure this question applies to other languages) I often create two classes that "know about" each other. More concretely, class A imports B, and class B imports class A, and both have member or local variables of the other type.
这被认为是不好的设计吗?如果愿意,可以使用反模式吗?
Is this considered bad design? An anti-pattern if you will?
推荐答案
这是我的看法:
- 如果两个类属于同一个逻辑模块,则可能很好(当然仍然是一个判断调用,有很多灰色区域).
- 如果这两个类属于不同的模块,那就不太好了.这在模块之间创建了循环依赖关系.我尽量避免这种情况,而是希望使用清晰的层次结构.
这篇关于是否将循环依赖项视为不良设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文