为什么使用 com.company.project 结构?

发布于 2024-09-15 14:30:46 字数 1432 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对你而言 2024-09-22 14:30:46

防止名称冲突是此类包结构的一个相当实际的原因。如果您使用自己拥有的真实域名,而其他人都以相同的角色使用其包名称,则发生冲突的可能性很小。

特别是。在 Java 世界中,这是“预期行为”。如果您想查找您正在使用的遗留库的文档,而没有人再记得它来自哪里,它也会有所帮助;-)

关于在这样的包结构中存储文件:在 Java 世界中,包实际上是文件夹(或 .jar 文件中的路径)所以:是的,很多人确实以这种方式存储文件。

这种结构的另一个实际优点是,您始终知道某个库是否是内部开发的。

Preventing name-clashes is a rather practical reason for package structures like this. If you're using real domain names that you own and everybody else uses their package names by the same role, clashes are highly unlikely.

Esp. in the Java world this is "expected behaviour". It also kind of helps if you want to find documentation for a legacy library you're using that no one can remember anymore where it was coming from ;-)

Regarding storing files in such a package structure: In the Java world packages are effectively folders (or paths within a .jar file) so: Yes, quite a few people do store their files that way.

Another practical advantage of such a structure is, that you always know if some library was developed in-house or not.

葬﹪忆之殇 2024-09-22 14:30:46

我经常跳过com。因为即使是小型组织也有多个 TLD,但在命名空间中包含所有者的名称绝对有用,因此当您开始加入第三方库时,不会出现命名空间冲突。

试想一下,周围会有多少个 Utility 或 Logging 命名空间,这里至少有 Foo.Logging 和 Bar.Logging,并且开发人员可以为一个命名空间别名:)

I often skip the com. as even small orgs have several TLDs, but definitely useful to have the owner's name in the namespace, so when you start onboarding third-party libraries, you don't have namespace clashes.

Just think how many Utility or Logging namespaces there would be around, here at least we have Foo.Logging and Bar.Logging, and the dev can alias one namespace away :)

多情癖 2024-09-22 14:30:46

如果您从自己拥有的域名开始,反向表达,那么只有在那之后您才能与遵循相同结构的其他人发生冲突,因为没有其他人拥有该域名。

它仅在某些平台上使用。

If you start with a domain name you own, expressed backwards, then it is only after that point that you can clash with anyone else following the same structure, as nobody else owns that domain name.

It's only used on some platforms.

吹梦到西洲 2024-09-22 14:30:46

几个原因是:

  • 使用域名可以更容易地实现唯一性,而无需添加新的注册表
  • 就分层结构而言,从主要到次要是很自然的

对于第二点,请考虑在分层文件结构中存储带日期记录的示例。将其分层排列为 YYYY/MM/DD 比使用 DD/MM/YYYY 更明智:在根级别,您会看到按年份组织记录的文件夹,然后按月上升到下一个级别,最后按天上升。以其他方式(在根级别上几天或几个月)可能会相当尴尬。

对于域名,通常是subsub.sub.domain.suffix,即从小到大。这就是为什么当将其转换为分层包名称时,您会得到suffix.domain.sub.subsub

对于第一点,这里摘录自Java语言规范第三版,它可以为这个包命名约定提供一些启示:

7.7 唯一包名称< /h3>

开发人员应采取措施,通过为广泛分发的包选择唯一的包名称,避免两个已发布的包具有相同名称的可能性。这样可以轻松、自动地安装和编目包。本节指定了生成此类唯一包名称的建议约定。鼓励 Java 平台的实现提供自动支持,将一组包从本地和临时包名称转换为此处描述的唯一名称格式。

如果不使用唯一的包名称,则可能会在远离冲突包创建点的地方出现包名称冲突。这可能会造成用户或程序员难以或不可能解决的情况。在包之间的交互受到限制的情况下,ClassLoader 类可用于将具有相同名称的包彼此隔离,但不是以对简单程序透明的方式。

首先,您可以通过拥有(或属于拥有)互联网域名的组织来形成唯一的软件包名称,例如 sun.com。然后,您可以逐个组件反转此名称,以获得(在本例中)com.sun,并将其用作包名称的前缀,使用组织内开发的约定来进一步管理包名称。

包的名称并不意味着暗示该包存储在 Internet 中的位置;例如,名为 edu.cmu.cs.bovik.cheese 的软件包不一定可以从 Internet 地址 cmu.educs.cmu.edu< 获取/code> 或来自 bovik.cs.cmu.edu建议的生成唯一包名称的约定只是在现有的、众所周知的唯一名称注册表之上搭载包命名约定的一种方法,而不必为包名称创建单独的注册表。 p>

Several reasons are:

  • Using domain names makes it easier to achieve uniqueness, without adding a new registry
  • As far as hierarchical structuring goes, going from major to minor is natural

For the second point, consider the example of storing dated records in a hierarchical file structure. It's much more sensible to arrange it hierarchically as YYYY/MM/DD than say DD/MM/YYYY: at the root level you see folders that organize records by year, then at the next level by month, and then finally by day. Doing it the other way (by days or months at the root level) would probably be rather awkward.

For domain names, it usually goes subsub.sub.domain.suffix, i.e. from minor to major. That's why when converting this to a hierarchical package name, you get suffix.domain.sub.subsub.

For the first point, here is an excerpt from Java Language Specification 3rd Edition that may shed some light into this package naming convention:

7.7 Unique Package Names

Developers should take steps to avoid the possibility of two published packages having the same name by choosing unique package names for packages that are widely distributed. This allows packages to be easily and automatically installed and catalogued. This section specifies a suggested convention for generating such unique package names. Implementations of the Java platform are encouraged to provide automatic support for converting a set of packages from local and casual package names to the unique name format described here.

If unique package names are not used, then package name conflicts may arise far from the point of creation of either of the conflicting packages. This may create a situation that is difficult or impossible for the user or programmer to resolve. The class ClassLoader can be used to isolate packages with the same name from each other in those cases where the packages will have constrained interactions, but not in a way that is transparent to a naïve program.

You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as sun.com. You then reverse this name, component by component, to obtain, in this example, com.sun, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names.

The name of a package is not meant to imply where the package is stored within the Internet; for example, a package named edu.cmu.cs.bovik.cheese is not necessarily obtainable from Internet address cmu.edu or from cs.cmu.edu or from bovik.cs.cmu.edu. The suggested convention for generating unique package names is merely a way to piggyback a package naming convention on top of an existing, widely known unique name registry instead of having to create a separate registry for package names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文