如何构建库项目的Java代码?

时间:2023-01-18 23:40:05

I have some years experience in developing Java applications right now, but it is the first time I develop a (Java 8) library which has to be used by external developers. Thus I do not wan't to expose anything (Classes, Methods, etc.) except the API.

我现在有几年开发Java应用程序的经验,但这是我第一次开发一个必须由外部开发人员使用的(Java 8)库。因此,除了API之外,我不想暴露任何东西(类,方法等)。

Thus I started implementing it in a single package and kept everything in default scope which is only internal.

因此,我开始在一个包中实现它,并将所有内容保存在默认范围内,这只是内部的。

Because implementing this needs many many classes which will maybe split up into more classes to ensure maintainability and testability that package becomes way to large to my mind. In a regular project I would have created plenty of subpackages to structure the code but this isn't possible because this would lead to internal classes having public scope.

因为实现它需要许多类,这些类可能会分成更多的类以确保可维护性和可测试性,因此包会成为我想到的方法。在常规项目中,我会创建大量子包来构造代码,但这是不可能的,因为这会导致内部类具有公共范围。

Do you have any ideas or know any ways (maybe involving the build system (maven) or other suggestions when using java 8) to make such code more structured without exposing more classes as needed? It's not that I couldn't live with a single package, it just doesn't feel right somehow.

您是否有任何想法或知道任何方式(可能涉及构建系统(maven)或其他建议使用java 8)使这些代码更加结构化而不需要根据需要暴露更多类?并不是说我不能忍受一个单独的包裹,它只是感觉不对。

2 个解决方案

#1


2  

this would lead to internal classes having public scope.

这将导致内部类具有公共范围。

Having developed such libraries for many years, this isn't as much of a problem as you might image as most developers will only use some of the documented classes and methods and don't go digging around in your code as they are aware the details might change and break their solution with an upgrade.

在开发了这样的库多年之后,这并不像你想象的那么大,因为大多数开发人员只会使用一些记录的类和方法,并且不会在你的代码中挖掘,因为他们知道细节可能会通过升级改变并破坏他们的解决方案。

In short, if it's not documented, it doesn't exists for most developers.

简而言之,如果没有记录,大多数开发人员都不存在。

Note: if developers wants to get into your code they can use reflection and making it private won't matter either. Or they can take a copy of the code and modify it and do whatever they like.

注意:如果开发人员希望进入您的代码,他们可以使用反射并将其设为私有也无关紧要。或者他们可以复制代码并修改它并做任何他们喜欢的事情。

All you are doing is preventing developers how what to get into your code but not use reflection to do which is a small number.

你所做的只是阻止开发人员如何进入你的代码,而不是使用反射来做少数。

Do you have any ideas or know any ways (maybe involving the build system (maven) or other suggestions) to make such code more sturctured?

您是否有任何想法或知道任何方式(可能涉及构建系统(maven)或其他建议)以使这些代码更加坚固?

Java 9's Jigsaw have greater control. It allows you to determine what will be exported outside your JAR. This way you can have public members which can be used anywhere except from outside your JAR.

Java 9的Jigsaw具有更强的控制力。它允许您确定将在JAR外部导出的内容。通过这种方式,您可以拥有可以在JAR外部使用的公共成员。

#2


1  

First keeping everything into a single package is not a good idea cause separation of concerns is one key concept. Furthermore it's hard to find the public API in your current state of the code.

首先将所有内容保存在一个包中并不是一个好主意,因为关注点分离是一个关键概念。此外,很难在您当前的代码状态中找到公共API。

This means you have a package for the API which results into having a single module whatever-api(artifact name) and one for the implemtation module-api-impl etc. or some more (later).

这意味着你有一个API包,它会产生一个单独的模块,无论是api(工件名称),还是一个用于实现模块-api-impl等,或者更多(稍后)。

This will also in resulting having separate packages and separates the code and tests etc. from each other and shows where you need to separate your code (may be you have coupled you core unintended.

这也将导致具有单独的包并将代码和测试等彼此分开,并显示您需要将代码分开的位置(可能是您已经无意中将核心耦合在一起)。

Exposing things is done yes. The current Java do not allow such correct separation and clear definition of an API etc. (also from a technical perspective) This can be done starting with Java 9 you will really be able to separate internal from public things by using appropriate modules. The other possible way would be to using OSGi which has such separation and and a strict separation of public api and implementation.

暴露事情是肯定的。当前的Java不允许这样的正确分离和API等的明确定义(也从技术角度来看)这可以从Java 9开始,您将能够通过使用适当的模块将内部事物与公共事物分开。另一种可能的方法是使用具有这种分离的OSGi,并严格分离公共API和实现。

#1


2  

this would lead to internal classes having public scope.

这将导致内部类具有公共范围。

Having developed such libraries for many years, this isn't as much of a problem as you might image as most developers will only use some of the documented classes and methods and don't go digging around in your code as they are aware the details might change and break their solution with an upgrade.

在开发了这样的库多年之后,这并不像你想象的那么大,因为大多数开发人员只会使用一些记录的类和方法,并且不会在你的代码中挖掘,因为他们知道细节可能会通过升级改变并破坏他们的解决方案。

In short, if it's not documented, it doesn't exists for most developers.

简而言之,如果没有记录,大多数开发人员都不存在。

Note: if developers wants to get into your code they can use reflection and making it private won't matter either. Or they can take a copy of the code and modify it and do whatever they like.

注意:如果开发人员希望进入您的代码,他们可以使用反射并将其设为私有也无关紧要。或者他们可以复制代码并修改它并做任何他们喜欢的事情。

All you are doing is preventing developers how what to get into your code but not use reflection to do which is a small number.

你所做的只是阻止开发人员如何进入你的代码,而不是使用反射来做少数。

Do you have any ideas or know any ways (maybe involving the build system (maven) or other suggestions) to make such code more sturctured?

您是否有任何想法或知道任何方式(可能涉及构建系统(maven)或其他建议)以使这些代码更加坚固?

Java 9's Jigsaw have greater control. It allows you to determine what will be exported outside your JAR. This way you can have public members which can be used anywhere except from outside your JAR.

Java 9的Jigsaw具有更强的控制力。它允许您确定将在JAR外部导出的内容。通过这种方式,您可以拥有可以在JAR外部使用的公共成员。

#2


1  

First keeping everything into a single package is not a good idea cause separation of concerns is one key concept. Furthermore it's hard to find the public API in your current state of the code.

首先将所有内容保存在一个包中并不是一个好主意,因为关注点分离是一个关键概念。此外,很难在您当前的代码状态中找到公共API。

This means you have a package for the API which results into having a single module whatever-api(artifact name) and one for the implemtation module-api-impl etc. or some more (later).

这意味着你有一个API包,它会产生一个单独的模块,无论是api(工件名称),还是一个用于实现模块-api-impl等,或者更多(稍后)。

This will also in resulting having separate packages and separates the code and tests etc. from each other and shows where you need to separate your code (may be you have coupled you core unintended.

这也将导致具有单独的包并将代码和测试等彼此分开,并显示您需要将代码分开的位置(可能是您已经无意中将核心耦合在一起)。

Exposing things is done yes. The current Java do not allow such correct separation and clear definition of an API etc. (also from a technical perspective) This can be done starting with Java 9 you will really be able to separate internal from public things by using appropriate modules. The other possible way would be to using OSGi which has such separation and and a strict separation of public api and implementation.

暴露事情是肯定的。当前的Java不允许这样的正确分离和API等的明确定义(也从技术角度来看)这可以从Java 9开始,您将能够通过使用适当的模块将内部事物与公共事物分开。另一种可能的方法是使用具有这种分离的OSGi,并严格分离公共API和实现。