当您在两个独立的应用程序中需要相同的类时,如何避免 Java 中的重复代码?

发布于 2025-01-12 09:16:44 字数 462 浏览 3 评论 0原文

为了简单起见,假设我有一个名为“Dog”的类。

然后我有两个独立的应用程序,其中包含该类的实现。

  1. 应用程序“A”对 Dog 类执行某些操作。
  2. 应用程序“B”对 Dog 类做了另一件事。

两个应用程序都需要 Dog 类,因此它会被重复。


现在让我们假设一下:

  1. 这些应用程序无法合并为一个。
  2. A”和“B”的实现不能移动到另一个应用程序中。

我正在使用 Spring Boot,如果该信息有帮助的话。

有没有办法解决 Dog 类的重复问题?

For the sake of simplicity, let's say that I have a class named "Dog".

Then I have two separated applications containing implementations of that class.

  1. Application "A" does something with the Dog class.
  2. Application "B" does another something with the Dog class.

Both applications require the Dog class, hence it would be duplicated.


Now let's say for a moment that:

  1. Those applications can't be merged into one.
  2. The implementations from "A" and "B" can't be moved into another application.

I'm using Spring Boot, if that info helps somehow.

Is there a way to fix the duplication of the Dog class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋意浓 2025-01-19 09:16:45

正如 @Slaw 所说,您可以创建一个 jar/库,然后使用任何构建工具(如 maven 或 gradle),并在构建时将 jar/库打包到存档文件中
这是更多信息的链接
https://christianlydemann.com/a-guide-to-sharing -项目之间的代码/

As @Slaw says you can create a jar/library then use any build tool like maven or gradle and package the jar/library in your archive file when it is built
and here is a link for more info
https://christianlydemann.com/a-guide-to-sharing-code-between-projects/

流心雨 2025-01-19 09:16:45

除了上述解决方案之外,您还可以使用 sonarqube 工具来识别代码重复。它会告诉您哪些代码是重复的以及如何修复它
https://docs.sonarqube.org/7.2/MetricDefinitions.html

In addition to above solution you can use sonarqube tool to identify code duplication . It will tell you which code is duplicate and how you can fix it
https://docs.sonarqube.org/7.2/MetricDefinitions.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文