Classes with inheritance and set method

Asked

Viewed 113 times

3

I’m having a doubt, I’m developing a simple modeling with a superclass person and two other subclasses physical person and legal person.

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 they are many parameters.. I need some dirt or a recommendation.

inserir a descrição da imagem aqui

  • What is this set he looks really weird. The get also a little, can explain better, mainly what return.

  • the set method is to register the name, address, etc... the get will return the values.

  • 4

    @Shouldn’t this be the constructor? The get/set methods are usually related to only one field of the class.

  • That doesn’t make sense.

  • 1

    Instead of the set, use the class constructor by passing the parameters. Another option is to search for a Pattern design called Builder, that serves for cases where your object has many fields. Here have an example.

  • 1

    Thank you all, I will follow the recommendation of the builder and I will read about the design Pattern Builder.

  • @Cleristonmartinscardoso Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

Show 2 more comments

2 answers

2

Pessoa is it abstract? Should.

You look like you could use a builder. Then the superclass has one and the subclasses have their own that get the data needed for it and the superclass.

The set should not be used for this. The get doesn’t look like it should be used for this.

There are even questions whether you should use this kind of artifice, even if you do it right.

Behold What good is a builder?. In some cases this may not be the solution, but it usually is. See more on How and when to build a valid state object?. May also be useful: One can override in builders?. And a example.

I wouldn’t do what Dherik would do, since that’s abstraction obssession, the opposite of Primitive obssession which he will surely say is what I do. I tend to be pragmatic and use what he needs, which gives me an advantage. 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. In another case I might even use it. Of course, Endereco I probably would, as I I answered the other AP question (which has already improved a lot), but not necessarily.

In UML it is difficult to show exactly the best form because it is something abstract, the culture and mechanisms of each language makes changing the form. And it’s a mistake when the person doesn’t understand this. For good people are abandoning UML, I never found it good, I do not buy fads without practical basis.

0

I wouldn’t recommend the set and yes the use of builders in your case.

In OOP, it is usually clearer to use small classes and start objects through their constructors. Use set for all fields leaves the object open to any kind of change of values at any time, which is normally not what is desired.

Beginning with Juridica, she could have the following fields in her builder:

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 methods set. As I said earlier, using smaller objects, with greater meaning, facilitates understanding.

Therefore, I would create 4 more classes: Endereco, Cpf, Rg, and Cnpj. Each with the following fields in their builders:

  • Addressee: endereco, bairro, cep, cidade, estado
  • Cpf: numero
  • Rg: numero
  • Cnpj: numero, tipo

See that now you have a more organized representation of the information that Fisica and Juridica receive.

Thus, the class Juridica would receive in your builder:

nome, Endereco, Cnpj

And in the Fisica:

nome, Endereco, Cpf, Rg
  • Wonder, what about get? I don’t know if it would be recommendable if I had several methods to get the values of the attributes.. For example, getNome, getEndereco, getCpf..

  • The method get you create only for the information you want to get later. That is, information that the object can provide for other objects. It may make sense to some fields, but not to others. In your case, it seems to make sense to everyone, I see no problem at all.

  • Thank you @Dherik

Browser other questions tagged

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