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

Asked

Viewed 177 times

5

If I create for example two classes and place the class Funcionario with a variable Empregado in class Empresa, what this means and what it serves?

class Funcionario {
    String nome;
    String cpf;
}

class Empresa {
   String nome;
   Funcionario empregado; 
}
  • 1

    It’s called composition.

  • What is the composition for?

  • In the link there is a very quiet explanation about it, see if it clarifies you.

  • 2
  • I’m not sure where your doubt lies, but at the most basic level, Funcionario is the member’s type empregado expected in instances of Empresa.

  • @Thiagodebonis the answer helped you? You know you can accept an answer in your questions, right? Something needs to be improved for the answer to be accepted?

Show 1 more comment

2 answers

6

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

6

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

In composition:

  • the life cycle of one object (Employee) is dependent on the life cycle of another (Company), so the creation of the Employee instance takes place within the Company class.

In the association:

  • the life cycle of an object is not dependent on the other (usually this relationship gives by methods, a method of the Company has as its parameter an Employee)

Browser other questions tagged

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