简体   繁体   English

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

[英]How to structure Java code of a library project?

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. 我现在有几年开发Java应用程序的经验,但这是我第一次开发必须由外部开发人员使用的(Java 8) Thus I do not wan't to expose anything (Classes, Methods, etc.) except the API. 因此,除了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? 您是否有任何想法或知道任何方式(使用Java 8时可能涉及到构建系统(Maven)或其他建议)来使此类代码更加结构化,而无需根据需要公开更多类? It's not that I couldn't live with a single package, it just doesn't feel right somehow. 这并不是说我不能忍受单个软件包,只是感觉不对。

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. Java 9的Jigsaw具有更大的控制权。 It allows you to determine what will be exported outside your JAR. 它使您可以确定将导出到JAR之外的内容。 This way you can have public members which can be used anywhere except from outside your JAR. 这样,您可以拥有可以在除JAR之外的任何地方使用的public成员。

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的程序包,该程序包导致具有单个模块whatever-api (工件名称)和一个用于实现module-api-impl等的module-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. 当前的Java不允许这样正确的分离和对API等的清晰定义(同样从技术角度而言)。这可以从Java 9开始完成,您实际上可以通过使用适当的模块将内部事物与公共事物分开。 The other possible way would be to using OSGi which has such separation and and a strict separation of public api and implementation. 另一种可能的方式是使用OSGi,它具有这样的分离以及对公共api和实现的严格分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM