为什么反向 DNS 表示法用于包命名?

Why is reverse DNS notation used for package naming?

使用反向 DNS 包符号有技术原因吗?还是只是约定俗成?

我认为它现在主要只是一个约定,但我想说它有助于将事情组织得井井有条。

以下是我为什么这么说的几个例子: com.vzy.gui.*com.vzy.io.*

  • 通过他们,我们看到,com 并想,“哦,嘿,这是一家公司 这使得这个
  • 然后 vzy 想想,“做这个的人是 vzy!"
  • 然后是 guiio 这让我们看到“这个与 GUI 和另一个与 IO 一起工作,很酷。"

我认为另一种展示这一点的好方法是将其切换回来,看看它的外观...

import ArrayList.util.java
import BorderLayout.awt.java
import File.io.java
import JButton.swing.javax
import JFrame.swing.javax
import JLabel.swing.javax
import JOptionPane.swing.javax
import JPanel.swing.javax
import JScrollPane.swing.javax
import JSlider.swing.javax
import LinkedBlockingQueue.concurrent.util.java
import PrintWriter.io.java
import Scanner.util.java
import SwingUtilities.swing.javax

import java.awt.BorderLayout
import java.io.File
import java.io.PrintWriter
import java.util.ArrayList
import java.util.Scanner
import java.util.concurrent.LinkedBlockingQueue
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JSlider
import javax.swing.SwingUtilities

没有技术原因,只是避免冲突的约定。以下非规范引用自Java Language Specification

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

You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as oracle.com. You then reverse this name, component by component, to obtain, in this example, com.oracle, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names. Such a convention might specify that certain package name components be division, department, project, machine, or login names.

...

The name of a package is not meant to imply where the package is stored on the Internet. 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.

之所以包名是域名的倒序,仅仅是因为包名是一个fully qualified name。每个包包含一组 classes/interfaces and/or 个子包,子包的名称必须以其包含的包为前缀以形成完全限定名称:

The fully qualified name of a named package that is a subpackage of another named package consists of the fully qualified name of the containing package, followed by ".", followed by the simple (member) name of the subpackage.

Sun 曾经维护 Java 编码标准指南 - 您可以在此处的 Oracle 站点上找到存档副本 - 根据该站点,自 1999 年以来就没有维护过: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

包含包名称建议的部分确实建议使用拥有组织的域名,顶级域优先: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

我认为这在一段时间内成为了公认的标准,尽管今天并没有严格遵守。没有理由不能为您的项目或组织设计一个对您最有意义的命名模式——没有技术原因为什么它必须遵循这种域名模式。

今天遵循的典型模式是首先是更高级别的分组(组织或项目名称),然后是越来越具体的分组,最后是名称中最具体的功能或技术分组。