Classes with inheritance and set method

3

I'm having a question, I'm developing a simple modeling with one person superclass and two other subclasses individual and corporate.

I'm confused about the SET method. I do not know if I should pass the values of the attributes per parameter at the time of the SET method call, since this is a lot of parameters. I need some compromise or recommendation.

    
asked by anonymous 03.07.2018 / 17:37

2 answers

2

Pessoa is abstract? Should.

It seems to be in need of a builder. Then the superclass has one and the subclasses have their own that get the data needed for it and the superclass.

set should not be used for this. get does not look like it should be used for this.

There's even questioning whether you should use this kind of trick, even if it works.

See What is a builder for? . In some cases this may not be the solution, but it usually is. See more at How and when to build a valid object? . It can also be useful: Can you override constructors? . And an example .

I would not do what Dherik would do, since this is abstraction obssession , the opposite of primitive obssession that will certainly tell you what I do. I tend to be pragmatic and use what I need, which gives me the edge. For this case I see no advantage and does not simplify anything, depending on the language I would even do more, so UML is almost a cancer, it does not consider whether the implementation has more facilities or benefits from its model. Otherwise he might even use it. Of course, Endereco would probably do it, as I answered in the other question of the AP (which has improved a lot) , but not necessarily.

In UML it is difficult to show exactly the best way because it is something abstract, the culture and mechanisms of each language makes shape change. And it is a mistake when the person does not realize it. For good people are abandoning UML, I never found this good, I do not buy fads without practical foundation.

    
03.07.2018 / 18:59
0

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
    
03.07.2018 / 19:16