What is the name of the concept used in this code?

4

If I create for example two classes and put the Funcionario class with a Empregado variable in the Empresa class, what is the meaning of this and what is it for?

class Funcionario {
    String nome;
    String cpf;
}

class Empresa {
   String nome;
   Funcionario empregado; 
}
    
asked by anonymous 18.07.2016 / 22:58

2 answers

6

In this example, in fact, you can not tell if it's an association or composition. You would need to know if this Employee object is instantiated within the Enterprise class or not.

In composition:

  • The life cycle of an object (employee) is dependent on the life cycle of another (company), so the creation of the function instance happens within the class.

In association:

  • The life cycle of one object is not dependent on the other (usually this relationship is given by methods, a Company method has as parameter an Employee)
09.10.2016 / 23:30
5

This seems to be a association (you would need more details to be sure, I'm using intuition). Eventually it could be a composition (again, it depends on details). And one day you may want to switch to aggregation , after all the company must have multiple employees.

    
18.07.2016 / 23:11