What are packages?

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 URL in reverse? What is it for? Can anyone explain me?

    
asked by anonymous 02.06.2015 / 15:08

1 answer

9

The package is nothing more than a way to define which entity owns the code and organize 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 also created a Pessoa class in a way and for a completely different purpose to use them. After that, Pedro needs to use the classes created by the Tabajara and XPTO organizations in his project, 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 classes of org.tabajara.Pessoa and XPTO of br.com.xpto.Pessoa .

But why relying on the domain name? Because in general, the domain can only have one owner, and therefore, if you own the xpto.com.br domain, using br.com.xpto then you will have a certain guarantee that no one will be colliding names with you. On the other hand, if you only used xpto as a package name, it might be that China's XPTO would make use of xpto also, instead of cn.com.xpto .

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

If you set the package to all of them 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 template in which the folders on your computer 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 the reverse, so by following this trend we have br/com/xpto/loja/persistencia .

One fact that reinforces this idea of putting the domain to the contrary 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 within xpto.com.br , and to maintain a consistency and follow the hierarchical folders model, the domain is inverted and both result in br/com/xpto/loja/persistencia .

To be honest, whoever has been defined in the reverse order is not the packet, but the domain. Disabling the domain returns the normal order.

Finally, there is a small though. This scheme works on the assumption that the domain used is yours. This scheme breaks when the domain you use is not yours, or when your domain is changed, so beware of those circumstances.

    
02.06.2015 / 16:27