如何从两个具体类型的类中删除循环依赖
我有两个类,如下所示:
public class A{
private String id ;
private SortedMap<String,B> answer = new TreeMap<String,B>();
private String text;
}
public class B{
private String id = null ;
private SortedMap<String,A> question = new TreeMap<String,A>();
private String text = null;
}
有什么方法可以从上述类中删除循环依赖..?
I have two classes which are as follows :
public class A{
private String id ;
private SortedMap<String,B> answer = new TreeMap<String,B>();
private String text;
}
public class B{
private String id = null ;
private SortedMap<String,A> question = new TreeMap<String,A>();
private String text = null;
}
Is there any way i can remove circular dependency from the above classes..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不,没有,但这没问题。
JAVA中循环依赖是没有问题的。如果您想在两个方向上遍历结构,那么拥有这些是很常见的。想象一棵树,其中父级和子级彼此了解,从而创建循环依赖关系。
垃圾收集器将检测循环依赖性并很好地处理这个问题。
唯一的问题发生在两个构造函数中都有循环依赖关系时,这将导致堆栈溢出:)
No there isn't but that's no problem.
There is no problem with having circular dependency in JAVA. It's quite common to have those if you want to traverse structures in both directions. Think a tree where the parent and child knows of each other thus creating a circular dependency.
The Garbage Collector will detect circular dependencies and handle this just fine.
The only problems occur when having circular dependencies in both constructors which will result in an stack overflow :)
不,除非您删除其中一张地图。
No, unless you remove one of the maps.
根据您所拥有的知识,您不需要两门课程。尝试使该类更加通用,而您只需要一个。
Based on what you have, you don't need two classes. Try to make the class more generic and you only need one.
如果您有两个相互引用的类,那么如果您同时编译这两个类,javac 将解决该问题:
但如果您输入:
它将解决循环编译器依赖关系。
If you have two classes which reference each other, javac will resolve that if you compile both at the same time:
but if you enter:
it will resolve the circular compiler dependency.
(这实际上是一条评论,但我没有足够的声誉点来做到这一点)
:->你为什么要这样做?
因为 findbugs 在 Pattern: CD_CIRCULAR_DEPENDENCY 中这么说:
该类与其他类存在循环依赖关系。这使得构建这些类变得困难,因为每个类都依赖于另一个类才能正确构建。考虑使用接口来打破硬依赖。
另外 维基百科 说:
...在软件设计中,较大软件模块之间的循环依赖关系
由于它们的负面影响而被认为是反模式......
循环依赖通常是由没有经验的程序员引入的......
(this is actually a comment, but I don't have enough reputation points to do that)
:->why would you like to do that?
because findbugs says so in Pattern: CD_CIRCULAR_DEPENDENCY:
This class has a circular dependency with other classes. This makes building these classes difficult, as each is dependent on the other to build correctly. Consider using interfaces to break the hard dependency.
also wikipedia says:
...in software design circular dependencies between larger software modules
are considered an anti-pattern because of their negative effects...
Circular dependencies are often introduced by inexperienced programmers...
如果您觉得需要,或者只是在 A 中使用布尔字段
if you feel you need, or just use a boolean field in A
回顾上一个问题 - 您可以更改 xml 架构并向答案添加某种
标记。那么等效的 xml 文档将是:您可能也想使用唯一的 id 作为答案,或者如果您想重用不同问题的答案(多对多),甚至可以再次反应模型 关系)
而你的类:
当然存在循环依赖,但这是绝对需要的,并且是从模型现实生活领域继承的。
Looking back to the previous question - you could change the xml schema and add some sort of
<nextquestion>
tag to answers. The equivalent xml document then would be:You may want to use unique ids for the answers too or even reactor the model again if you want to reuse answers for different questions (many-to-many relationship)
And you classes:
And of course there is a circular dependency but that is absolutely wanted and inherited from the models real-life domain.
尝试类似的东西:
try something like: