Two java libraries importing each other?

…衆ロ難τιáo~ 提交于 2019-12-12 09:44:46

问题


I am working on a legacy framework and apparently there are two libraries, which are inter-dependent. By that I mean libA import from libB, and libB import from libA. First i think it is a terrible design, but why would somebody do something like this? Rather which conditions can lead somebody to write this ?

edit:

Each library depends on classes in the other, so they do import packages and have the other library jar in their build path.


回答1:


It's easier to do in this case, because the two parties are independent. If they don't talk to each other, it's not hard to create cycles. You have to be mindful to avoid them.

Cyclic dependencies aren't hard to create. Look at Java itself: java.lang, java.util, and java.io have cycles. Will you stop writing Java, since it's so "terrible"?

It means that you can never use libA without libB and vice versa. They've become one big library. Same with packages in Java and other systems: once you have a cycle, you have to use all those packages together as if they were one.

The guys who write Spring pay a lot of attention to cycles. They design and refactor their framework to eliminate them.

So - what's the harm? Juergen Heller says they're bad, and he's right. But from your point of view, what evil is visited upon you? It means you have to use both when you run and test. You can't test class A without class B and vice versa when there's a cycle between them. It makes testing and running harder.

You can choose an alternative that doesn't have the cycle. If you can change the source, you can refactor and maintain it. But that's it.

You should check your own code to see if you've done it to yourself. IntelliJ has nice analysis tools which can be applied to a code base. Check it out.




回答2:


While developing lib A, the developer found that the class Foo from lib B was useful. And while developing lib B, the developer found that the class Bar from lib A was useful.

I'm not saying it's a wise thing to do, but your question asks why anybody would do that. This is probably the answer.




回答3:


Both the libraries were written at a the same time, possibly by different developers. Or the same developer at different times, or by a developer who treated both libraries as one big code base, and wasn't concerned about avoiding circular dependencies. e.g. They had a hard enough time writing something which worked without worrying about niceties.




回答4:


Most likely it will be inexperience.
Modern build tools like Maven preclude circular dependencies between artifacts.



来源:https://stackoverflow.com/questions/8833480/two-java-libraries-importing-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!