(Java)软件包组织有最佳实践吗? [英] Are there best practices for (Java) package organization?

查看:81
本文介绍了(Java)软件包组织有最佳实践吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前,我在这里看到一个有关Java包的细粒度组织的问题.例如my.project.utilmy.project.factorymy.project.service

A little while ago, I saw a question answered here regarding the fine-grained organization of java packages. For example, my.project.util, my.project.factory, my.project.service, etc.

我现在找不到它,所以我不妨问这个问题.

I can't find it now, so I may as well ask the question.

是否有关于Java包组织及其最佳实践的最佳实践?

Are there best practices with regards to the organization of packages in Java and what goes in them?

您如何组织Java项目中的类?

How do you organize your classes in your Java project?

例如,我正在与几个人合作的一个项目有一个名为bean的软件包.它最初是一个包含简单bean的项目,但最终(由于经验不足和时间不足)(几乎)包含了所有内容.我通过将一些工厂类放入工厂包中(使用创建bean的静态方法的类)对它们进行了一些清理,但是我们还有其他一些类在执行业务逻辑,而其他一些类则在进行简单的处理(而不是使用业务逻辑),例如检索来自属性文件的代码消息.

For instance, a project I'm working on with a few people has a package called beans. It started out being a project containing simple beans, but has ended up (through poor experience and lack of time) containing everything (almost). I've cleaned them up a little, by putting some factory classes in a factory package (classes with static methods that create beans) but we have other classes that do business logic and others that do simple processing (not with business logic) like retrieving a message for a code from a properties file.

您的想法和意见受到赞赏.

Your thoughts and comments are appreciated.

推荐答案

包装组织或包装结构通常是激烈的讨论.以下是一些简单的程序包命名和结构准则:

Package organization or package structuring is usually a heated discussion. Below are some simple guidelines for package naming and structuring:

  • 遵循java 包命名约定
  • 根据包的功能角色和业务角色来构建包
    • 根据其功能或模块细分软件包.例如com.company.product.modulea
    • 进一步的细分可能基于您软件中的层次.但是,如果程序包中只有很少的类,请不要着急,那么将所有内容都包含在程序包中是有意义的.例如com.company.product.module.webcom.company.product.module.util
    • 除非迫切需要,否则避免过度进行结构设计,IMO避免为例外情况,工厂等单独包装.
    • Follow java package naming conventions
    • Structure your packages according to their functional role as well as their business role
      • Break down your packages according to their functionality or modules. e.g. com.company.product.modulea
      • Further break down could be based on layers in your software. But don't go overboard if you have only few classes in the package, then it makes sense to have everything in the package. e.g. com.company.product.module.web or com.company.product.module.util etc.
      • Avoid going overboard with structuring, IMO avoid separate packaging for exceptions, factories, etc. unless there's a pressing need.

      经过几次试验和尝试,您应该能够找到自己喜欢的结构.不要只拘泥于一种约定,可以随时更改.

      After a few experiments and trials you should be able to come up with a structuring that you are comfortable with. Don't be fixated on one convention, be open to changes.

      这篇关于(Java)软件包组织有最佳实践吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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