这是什么意思以及如何修复 SonarQube Java 问题“应删除包之间的循环";(鱿鱼:CycleBetweenPackages) [英] What does it mean and how to fix SonarQube Java issue "Cycles between packages should be removed" (squid:CycleBetweenPackages)
问题描述
当这些包中的类之间存在使用或导入种类的依赖时,包之间存在循环.
Cycles exist between packages when there are dependencies of using or importing kind between classes in these packages.
考虑以下示例.让有 4 个类:Truck
和 Car
接口在 org.example.car
包和 Navigation
和 >CPU
在包 org.example.part
中.在包 org.example.car
和 org.example.part
中,我们使用了类 Truck --> 之间的关系.汽车
和类Navigation -->CPU
.让我们假设 Truck
类使用 Navigation
类,所以我们有数据包之间的关系 org.example.car -->org.example.part
.然后假设CPU
类也使用了Car
接口,这给了我们包org.example.part -->org.example.car
.尽管 Navigation
类不使用任何包 org.example.car
类,但我们在包中的相邻类上有一个循环(CPU
类).
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).
这个例子如下图所示:
alt-cycles-in-packages-example.svg-cycles
当一个循环中涉及多个包时,这意味着这些包是高度耦合的,如果不导入所有其他包,就无法重用/提取其中一个包.这样的循环可能会迅速增加维护应用程序和接受业务变化所需的工作量.
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.
推荐答案
要修复包中的依赖循环,您应该引入额外的包并将几个依赖类/接口移动到新创建的包中.
To fix dependency cycle in your packages you should introduce additional package and move couple of dependent classes/interfaces to that newly created package.
如下图所示:
a
在我们的示例中,让我们选择将接口 Car
移动到新创建的包 org.example.api
.这打破了依赖循环并降低了我们应用程序中的技术深度.现在所有包都只有一种依赖关系,如果需要,可以将它们拆分为不同的工件.
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.
这篇关于这是什么意思以及如何修复 SonarQube Java 问题“应删除包之间的循环";(鱿鱼:CycleBetweenPackages)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!