I would not recommend set
, but rather the use of builders in your case.
In OOP, it usually becomes clearer to use small classes and to start the objects through their constructors. Using set
for all fields leaves the object open to any type of change of values at any time, which is not usually what you want.
Beginning with Juridica
, it could have the following fields in its constructor:
nome, endereco, bairro, cep, cidade, estado, cnpj, tipo
And Fisica
:
nome, endereco, bairro, cep, cidade, estado, rg, cpf
However, we can improve this because we still have many parameters instead of many set
methods. As I said earlier, using smaller, more meaningful objects makes it easier to understand.
So, I would create 4 more classes: Endereco
, Cpf
, Rg
, and Cnpj
. Each with the following fields in its constructors:
- Address:
endereco, bairro, cep, cidade, estado
- Cpf:
numero
- Rg:
numero
- Cnpj:
numero, tipo
See that you now have a more organized representation of the information that Fisica
and Juridica
receive.
So, the class Juridica
would receive in its constructor:
nome, Endereco, Cnpj
And no Fisica
:
nome, Endereco, Cpf, Rg