问题
Cycles exist between packages when there are dependencies of using or importing kind between classes in these packages.
Consider the following example. Let there be 4 classes: Truck
and interface Car
in org.example.car
package and Navigation
and CPU
in package org.example.part
. In packages org.example.car
and org.example.part
we have use relations between classes Truck --> Car
and classes Navigation --> CPU
. Let's assume that the class Truck
use Navigation
class so we have the relationship between packets org.example.car --> org.example.part
. Then assume that the class CPU
also uses the Car
interface, which gives us the relationship between packages org.example.part --> org.example.car
. Although the class Navigation
does not use any class of packet org.example.car
we have a cycle on neighboring classes in packages (CPU
class).
This example is presented in figure below:
When several packages are involved in a cycle, that means those packages are highly coupled, and there is no way to reuse/extract one of those packages without importing all the other packages. Such cycle could quickly increase the effort required to maintain an application and embrace business change.
回答1:
To fix dependency cycle in your packages you should introduce additional package and move couple of dependent classes/interfaces to that newly created package.
This is presented in figure below:
In our example, let us chose to move interface Car
to newly created package org.example.api
. That breaks the dependency cycle and lowers technical dept in our application. Now all packages have dependencies in only one way and it is possible to split them in different artifacts if needed.
来源:https://stackoverflow.com/questions/34899712/what-does-it-mean-and-how-to-fix-sonarqube-java-issue-cycles-between-packages-s