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.
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 :)
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.
包的名称并不意味着暗示该包存储在 Internet 中的位置;例如,名为 edu.cmu.cs.bovik.cheese 的软件包不一定可以从 Internet 地址 cmu.edu 或 cs.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:
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.
发布评论
评论(4)
防止名称冲突是此类包结构的一个相当实际的原因。如果您使用自己拥有的真实域名,而其他人都以相同的角色使用其包名称,则发生冲突的可能性很小。
特别是。在 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.
我经常跳过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 :)
如果您从自己拥有的域名开始,反向表达,那么只有在那之后您才能与遵循相同结构的其他人发生冲突,因为没有其他人拥有该域名。
它仅在某些平台上使用。
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.
几个原因是:
对于第二点,请考虑在分层文件结构中存储带日期记录的示例。将其分层排列为
YYYY/MM/DD
比使用DD/MM/YYYY
更明智:在根级别,您会看到按年份组织记录的文件夹,然后按月上升到下一个级别,最后按天上升。以其他方式(在根级别上几天或几个月)可能会相当尴尬。对于域名,通常是
subsub.sub.domain.suffix
,即从小到大。这就是为什么当将其转换为分层包名称时,您会得到suffix.domain.sub.subsub
。对于第一点,这里摘录自Java语言规范第三版,它可以为这个包命名约定提供一些启示:
Several reasons are:
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 sayDD/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 getsuffix.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: