处理循环依赖 [英] Dealing with a Circular Dependency
问题描述
我想知道是否有人可以建议在Java中打破两个类之间的循环依赖关系.FindBugs建议使用接口,所以我想知道是否有人对这类问题有任何良好的经验。
I wonder if someone can advise on any good ways to break a circular dependency between 2 classes in Java.FindBugs proposes the use of interfaces so i wonder if someone has any good experience with this type of problem.
推荐答案
并不总是要避免循环依赖。我会在大的地方避开它们,但要保持在系统的小角落里。在大型,即数据访问层和J2EE应用程序循环依赖的表示层,我会说这是一件坏事,因为它意味着一切都必须一次编译,测试是噩梦。但是,如果列表数据结构及其迭代器类型是循环依赖,则没有问题。
Circular dependencies aren't always to be avoided. I'd avoid them in the large, but keeps in small tight corners of a system. In the large, i.e if data access layer and the representation layer of J2EE app circular dependent, I'd say that is a bad thing, because it means that everything has to be compiled in one go and testing is nightmare. But, it's no problem if a list data-structure and its iterator type are circular dependend.
由于Findbugs建议使用接口来打破循环依赖。即,为至少一种类型的圆引入接口,并使其他类在任何地方使用该接口。你需要示例代码吗?
As Findbugs suggests use interfaces to break a circular dependency. I.e introduce a interface for at least one type of the circle and make the other classes use the interface everywhere. Do you need example code?
这篇关于处理循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!