Organizing packages in a java project

Asked

Viewed 1,446 times

21

When creating a project on Android, the IDE itself suggests that the main package has its own naming type (with.example.namepackaging), and also creates a whole hierarchy of directories already defined.

Just as it happens in Android, there is some convention to follow when programming in java (either desktop or web), about organization and nomenclature of application packages, or only MVC is applicable for this type of organization?

1 answer

19


Oracle itself has defined certain conventions to avoid conflicts between Java classes of different programmers/companies/projects.

It suggests that a company (or any programmer) use their own inverted web domain as their package name (since the domain is unique). Example: my company owns the domain www.empresaficticia.com.br. The main package of my projects would be defined as follows::

package br.com.empresaficticia;

For cases where the web domain is not a valid package name (has hyphen, dots, starts with digits, etc.), Oracle suggests using the underscore _. Example: my domain is www.empresa-ficticia.com.br, then my package would be:

package br.com.empresa_ficticia;

In addition to this top level organization suggested by Oracle, the rest basically becomes the opinion and preference of each programmer/company. A tip for when your project gets too big is to separate it into modules that are only one category of functionality. Example: You have X classes in your project that only handle database access, so put them in a sub-package db:

package br.com.empresa_ficticia.db;

and another sub-package could be called package br.com.empresa_ficticia.fs; and would contain its classes dealing with operating system files.

And a final tip, take a look at large open source projects. Analyze how they are organized. Some java projects with which I have already had contact and recommend to have a better idea of the subject: Apache Hadoop, Apache Giraph and Apache Hama.

But before you go out creating packets with names that have 40 characters, an invocation spell, and the blood of a virgin, analyze if this is really necessary. If you are just doing a pet project (project to learn something new, which will not really be used by anyone), it will hardly be necessary to encapsulate your project in a package.

Reference: https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.