我应该如何构造Java应用程序,将类放在哪里? [英] How should I structure a Java application, where do I put my classes?

查看:138
本文介绍了我应该如何构造Java应用程序,将类放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道如何构建Java应用程序.但是我总是对在哪里上课感到困惑.支持以严格面向领域的方式组织软件包的支持者,其他人则按层分开.

First of all, I know how to build a Java application. But I have always been puzzled about where to put my classes. There are proponents for organizing the packages in a strictly domain oriented fashion, others separate by tier.

我自己一直遇到

  • 命名,
  • 放置

所以

  1. 您将域特定的常量放在哪里(以及此类的最佳名称是什么?)?
  2. 您在哪里放置基础结构和领域特定的东西的类(例如,我有一个FileStorageStrategy类,该文件将文件存储在数据库中,或者存储在数据库中)?
  3. 在哪里放置例外?
  4. 有什么我可以参考的标准吗?

推荐答案

我真的很喜欢Maven的

I've really come to like Maven's Standard Directory Layout.

对我来说,主要思想之一是拥有两个源代码根目录-一个用于生产代码,一个用于测试代码,如下所示:

One of the key ideas for me is to have two source roots - one for production code and one for test code like so:

MyProject/src/main/java/com/acme/Widget.java
MyProject/src/test/java/com/acme/WidgetTest.java

(这里src/main/java和src/test/java都是源根).

(here, both src/main/java and src/test/java are source roots).

优势:

  • 您的测试具有对被测类的程序包级别(或默认")访问权限.
  • 通过将src/test/java作为源根目录,可以轻松地仅将生产源文件打包到JAR中.

关于类放置和程序包的一条经验法则:

One rule of thumb about class placement and packages:

通常来说,结构良好的项目将没有循环依赖项.了解什么时候不好(什么时候不是),并考虑使用 SonarJ 消除它们.

Generally speaking, well structured projects will be free of circular dependencies. Learn when they are bad (and when they are not), and consider a tool like JDepend or SonarJ that will help you eliminate them.

这篇关于我应该如何构造Java应用程序,将类放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆