简体   繁体   English

构建我们的Java包

[英]structuring our Java packages

This is some experimental thinking going on here about how to layout your Java packages. 这是关于如何布局Java包的一些实验性思考。

As Java developers we have seem the following layout in one form or another probably a gazillion times: 作为Java开发人员,我们似乎以一种或另一种形式编写了以下布局,可能需要花费数百万亿次。

com.example.service.rest  
UserRestService.java  
BookRestService.java  
TaskRestService.java  

com.example.service.rest.impl  
UserRestServiceImpl.java  
BookRestServiceImpl.java  
TaskRestServiceImpl.java  

com.example.data  
UserDTO.java  
BookDTO.java  
TaskDTO.java

I have been a consumer of such a layout for years. 多年来,我一直是这种布局的消费者。

I was briefly involved in a NodeJS project where the layout was completely different and I just loved the flatness, the simplicity, and the containment of every component. 我短暂地参与了一个NodeJS项目,该项目的布局完全不同,我只是喜欢每个组件的平坦性,简单性和包含性。

In comparison to what I got used to it felt like my classes are ( the above layout ) thrown about as if an exp;lotion taken place. 与我习惯的相比,我的类(上面的布局)好像发生了exp; lotion。

The NodeJS project layout ( translated to Java ) would look like this: NodeJS项目布局(转换为Java)如下所示:

com.example.service.rest.user  
UserDTO.java  
UserRestService.java  
UserRestServiceImpl.java  

com.example.service.rest.book  
BookDTO.java  
BookRestService.java  
BookRestServiceImpl.java  

com.example.service.rest.task  
TaskDTO.java  
TaskRestService.java  
TaskRestServiceImpl.java  

The NodeJS layout I adopted this from had a very flat structure where every directory / package would contain the rest controller, the service, and the DAL/DAO. 我从中采用的NodeJS布局具有非常扁平的结构,其中每个目录/包都将包含其余的控制器,服务和DAL / DAO。

Felt like everything was broken into organized micro buckets. 感觉就像一切都被分解成有组织的微型存储桶。

Has anyone ever tried the second approach on a larger scale before? 有人曾经尝试过更大规模的第二种方法吗?
Any thoughts you have on the first layout vs the second layout? 您对第一种布局与第二种布局有什么想法?
Any reading resources on the subject? 关于这个主题有阅读资源吗?

You can lay out your packages any way that makes sense. 您可以以任何合理的方式布置包裹。 What you called a "NodeJS" layout is quite common in Java code. 您所谓的“ NodeJS”布局在Java代码中很常见。 Each API might have its own logic driving package organization. 每个API可能都有自己的逻辑驱动程序包组织。 It's part of the art of computer programming to choose a package layout that supports maintenance effectively. 选择有效支持维护的程序包布局是计算机编程技术的一部分。

I really don't see anything essentially different between the one strategy and the other. 我真的没有看到一种策略与另一种策略有本质上的区别。 They're both arbitrary and could serve different projects. 它们都是任意的,可以服务于不同的项目。 I suggest that you use the one that makes the most sense for you, the team, and the individual project. 我建议您使用最适合您,团队和单个项目的方案。

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

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