问题描述
当在这些软件包中的类之间使用或导入种类存在依赖性时,软件包之间存在循环。
Cycles exist between packages when there are dependencies of using or importing kind between classes in these packages.
请考虑以下示例。假设有4类:中的接口 Car code>包, org.example.part <包中的 Navigation 和 CPU / code>。在包 org.example.car 和 org.example.part 的包中,我们使用类卡车->汽车和类导航-> 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).
此示例如下图所示:
当一个循环涉及多个软件包时,这意味着这些软件包是高度耦合的,没有这些方法,就无法重用/提取其中一个软件包导入所有其他软件包。这样的周期可能会迅速增加维护应用程序和进行业务变更所需的工作。
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.
如下图所示:
In在我们的示例中,让我们选择将界面 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!