当您在两个独立的应用程序中需要相同的类时,如何避免 Java 中的重复代码?
为了简单起见,假设我有一个名为“Dog”的类。
然后我有两个独立的应用程序,其中包含该类的实现。
- 应用程序“A”对 Dog 类执行某些操作。
- 应用程序“B”对 Dog 类做了另一件事。
两个应用程序都需要 Dog 类,因此它会被重复。
现在让我们假设一下:
- 这些应用程序无法合并为一个。
- “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.
- Application "A" does something with the Dog class.
- 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:
- Those applications can't be merged into one.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @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/
除了上述解决方案之外,您还可以使用 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