What are the packages?

Asked

Viewed 697 times

5

I’m starting with Phonegap and creating a project has the following structure:

phonegap create helloWorld com.dominio.pasta HelloWorld

But I never understood about this package. Is it a reverse URL? What’s it for? Can someone explain it to me?

1 answer

9


The package is nothing more than a way of defining which entity owns the code and organizing the code into logical groups within this entity.

Why? Mainly to avoid name collisions. Let’s assume that Tabajara organizations have created a class called Pessoa for their projects. On the other hand, the XPTO organization has also created a class Pessoa in a way and for a completely different purpose for their use. After that, Pedro needs to use in his project the classes created by Tabajara organizations and XPTO, but this would cause a problem, because there would be two classes called Pessoa.

The solution to this would be to use the package. Tabajara organizations could call their classes org.tabajara.Pessoa and the EXPOSURE of br.com.xpto.Pessoa.

But why rely on the domain name? Because in general, the domain can only have a single owner, and therefore if you own the domain xpto.com.br, when using br.com.xpto then you will have a certain assurance that no one will be bumping names with you. On the other hand, if you only used xpto as a package name, it may be that China’s XPTO invented using xpto also, instead of cn.com.xpto.

Why is the domain name reverse? Because of the hierarchy structure. Let’s assume you own the domain xpto.com.br, and in your company you have several projects. You have the design of the virtual store, customer registration, and the issuance of accounting files. Inside the virtual store, let’s assume that you have a data persistence module, a payment processing module and a web service module.

If you set the package of them all as just xpto.com.br, the result will be a mess, so you’ll want to use a hierarchy like xpto.com.br/loja/persistencia, xpto.com.br/loja/web, xpto.com.br/loja/pagamentos, xpto.com.br/cadastroclientes. This hierarchy structure follows the same model where your computer folders are modeled, which would not work if your folders were persistencia/loja/xpto/com/br for example. What happens is that persistencia is a module of loja which is a module of xpto.com.br. However, the domain name itself is also hierarchical, but it is in reverse, so following this trend we have, br/com/xpto/loja/persistencia.

One fact that reinforces this idea of putting the domain backwards is that in many places, there are subdomains within the same domain whose hierarchy is equivalent to the hierarchy of folders. For example, I could use loja.xpto.com.br or xpto.com.br/loja, but in both cases loja is inside xpto.com.br, and to maintain consistency and follow the hierarchical folder model, the domain is reversed and both result in br/com/xpto/loja/persistencia.

To tell the truth, who was defined in the reverse order, is not the package, but the domain. When the domain is uninjected, the normal order is returned.

Finally, there is a small but. This scheme works on the assumption that the domain used is yours. This schema breaks when the domain used is not yours, or when your domain is changed, so beware of these circumstances.

  • +1. Very good answer. Wrote everything I was writing and more..

  • Man, super explained. Thank you so much!

Browser other questions tagged

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