我应该如何构建一个Java应用程序,换句话说:我在哪里把我的课? [英] How should I structure a Java application, in other words: where do I put my classes?

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

问题描述

首先,我知道如何建立一个Java应用程序。但我一直很疑惑在哪里把我的班。有在严格的面向领域的时尚组织包支持者,其他人按层分离。

我自己一直与命名有问题),B)与配售


  1. 你在哪里把你的特定领域常数(,什么是最好的名字对于这样一个类)?

  2. 你在哪里把类的东西这既是基础设施和特定领域(比如我有一个FileStorageStrategy类,存储无论是在数据库中,或者在数据库中的文件)?

  3. 如果把例外?

  4. 是否有相应的标准,我可以参考?


解决方案

我真的变得喜欢Maven的<一个href=\"http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html\"相对=nofollow称号=S>标准目录布局。

一个对我的主要观点是有两个源根 - 一个用于生产code,一个用于测试code,像这样:

  MyProject的/ src目录/主/ JAVA / COM / ACME / Widget.java
MyProject的/ src目录/测试/ JAVA / COM / ACME / WidgetTest.ava

(在这里,无论是钢骨混凝土/主/ JAVA和src / test / java下为源根)。

优点:


  • 您的测试都封装(或默认)的水平,以你的类测试访问。

  • 您可以很容易地通过删除的src /测试/ Java作为源根包只生产源成JAR。

关于分班和包一个经验法则:

一般而言,结构良好的项目将是免费的循环依赖。学习的时候他们是坏的(当他们的),并考虑像<工具一个href=\"http://www.google.ca/search?q=JDepend&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a\"相对=nofollow> JDepend 或 SonarJ 这将帮助你消除它们。

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.

I myself have always had problems with a) with naming, b) with placing

  1. Where do you put your domain specific constants (and what is the best name for such a class)?
  2. Where do you put classes for stuff which is both infrastructural and domain specific (for instance I have a FileStorageStrategy class, which stores the files either in the database, or alternatively in database)?
  3. Where to put Exceptions?
  4. Are there any standards to which I can refer?

解决方案

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.ava

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

Advantages:

  • Your tests have package (or "default") level access to your classes under test.
  • You can easily package only your production sources into a JAR by dropping src/test/java as a source root.

One rule of thumb about class placement and packages:

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天全站免登陆