Organize packages in a java project

20

When you create a project in Android , the IDE itself suggests that the main package has its own naming type (com.example.packagedname), and also creates an entire hierarchy of directories already defined.

Just like in Android , is there a convention to follow when programming in java (whether desktop or web), organization and naming of application packages, or is MVC only applicable to this type of organization? / p>     

asked by anonymous 20.02.2016 / 13:07

1 answer

18

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) uses its own inverted web domain as the name of its packages (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, periods, starts with digits, etc.), Oracle suggests using underscore _ . Example: my domain is www.empresa-ficticia.com.br , so 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. One tip for when your project gets too big is to separate it into modules that deal with only one category of features. Example: you have X classes in your project that deal only with database access, then put them in a db sub-package:

package br.com.empresa_ficticia.db;

and another subpackage could be called package br.com.empresa_ficticia.fs; and would contain its classes that deal with operating system files.

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

But before you go out creating packages with names that have 40 characters, an invocation spell and the blood of a virgin, check if this is really necessary. If you are just doing a pet project, it will hardly be necessary to encapsulate your project in a package.

Reference: link

    
21.02.2016 / 15:46