Why do we use reverse domain name like com.something. or org.something. structure for java packages? I understand this brings in some sort of uniqueness, but why do we need this uniqueness?
Reverse domain name notation (or reverse-DNS) is a naming convention for components, packages, types or file names used by a programming language, system or framework. Reverse-DNS strings are based on registered domain names, with the order of the components reversed for grouping purposes.
Host names existed before domain names. When domains were bolted on they used the idea of a default domain for each host and that made sense to be on the end. Since people knew the host names and were used to dealing with them, the suffix was more natural since it kept the domain cruft out at the edge.
Usually domain sub-package is used to place domain objects (or model objects) mainly in projects implemented around MVC pattern, but not only. Model objects could map tables in your DB (if you are using an ORM) or they are just classes that represent the entities involved in your application logic.
Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .
About why we do it reversed: Imagine you have two important packages, an accounting package and a graphics package. If you specified these in 'straight' order:
accounting.mycompany.org graphics.mycompany.org
Then it implies there is a major accounting
package, a subsection of which is for mycompany
, and a subsection of that package is called the org
package which you actually use. However, you want this:
org.mycompany.accounting org.mycompany.graphics
This makes more sense. Out of all packages from organizations (org
), you look at mycompany
in particular, and it has two sub-packages, the accounting
and the graphics
ones.
Globally unique package names avoid naming collisions between libraries from different sources. Rather than creating a new central database of global names, the domain name registry is used. From the JLS:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With