Classes with associations in UML

Asked

Viewed 1,106 times

2

I’m from the time of structured programming and now I’m returning and trying to fit in with OOP. I made a diagram below. I’m on the right track?

I created this class Empresa thinking of not having to be limited in both classes PessoaJuridica and PessoaFfisica.

Diagrama de classes

  • Does it make sense to have a Person without a CPF or CNPJ? If not, the Pessoa class should be abstract. And what would Company be? Why does she inherit from both people? If she is both, it wouldn’t make sense to have four classes; concentrating everything in Person would be much simpler. Missing field Contacts in Person to make association.

  • 2

    And I wouldn’t put zip code, CNPJ, RG and CPF as integers. They’re strings composed only of numeric characters but not numerical values.

  • I would put at least the last two as classes, because they are data that have associated behavior (i.e., the validation of this data). For example, CPF has a validation of the last two digits per module-11. It can be argued that address could be a separate entity (class), but I have no experience in implementing this type of scenario to justify. It doesn’t make any sense that Company inherits from an Individual. Tell what you really want to do for the staff to give suggestions of how it can be done.

  • @Cleriston, you’re on the right track yes, you’ve improved a lot since your last question. The staff questions are interesting regarding the types of data used, we can go deeper into this now. Arriving home I can answer you more calmly. What would be the tipo? I think it’s time to separate the address information into one class Endereco. It is also good to use class builders, it is clearer to instantiate them and know their dependencies.

  • Thank you I will try to explain better the scenario I have in mind. I will have a client who can be a natural or legal person, the "type" represents another specific characteristic of the situation, for example, the client can be a "Mason" or a "Painter", My idea regarding the company class is for me to have only the client object(Company) and not differentiate between juridical and physical. If you have another suggestion I appreciate. Already the contact class represents that every customer can have 0 or multiple contacts like email or phone.

  • 1

    @Cleristonmartinscardoso, got it. This Empresa became a little strange, because a PF may have company and PJ can be a company. If I may say so, I would not work with so many heritages but with composition, but from what I understand from your context some people expect you to use inheritance, so I will try to help you in this line of thought.

  • @Dherik I thank you for your help. What do you think of having a company with physical and juridical person as composition?

  • @Cleristonmartinscardoso, provided that PF and PJ have a company (and not the other way around), yes.

  • 1

    Sorry, I was a little short and thick in my comment. And I do not have so much experience, here is my apology. I also hope to learn from this example that we are discussing here. May I ask what is the scope of the project? (if it is a customer register, with the profession of each, or some other context?)

  • In other words, what is the field of application?

  • @Piovezan, I will have a register of companies clients and partners, these companies can be physical and legal person, each one will have the specific characteristics are forwarders or shipowners.. These companies will be customers or suppliers of commercial proposals that have entered into another scope. In the future, I intend to implement a register of salespeople and employees.

  • Name should be in Person or in child classes? Shipowner and dispatcher are that? Which other "people" are involved? How to insert them into the model? Shouldn’t modeling be first conceptual and only later in the class diagram? Shouldn’t you start from use cases? Now I understand why is broad.

  • To choose the role modeling (I suggest the standard 4): http://www.jot.fm/issues/issue_2002_09/column2/

  • @Cleristonmartinscardoso The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

Show 9 more comments

3 answers

4

What about the time of structured programming? Has something changed that I don’t know? I still do structured programming. Maybe I’m a little confused about paradigms. One thing doesn’t eliminate the other. Even I would avoid assuming new paradigms without understanding this foundation and understanding why you are adopting a new paradigm in your project. Doing it because it’s fashionable is not a good reason.

Was there any problem and now doing OOP solves it? If not, there is no reason to change. Did wrong in procedural? Will do OO right? It’s harder to do object-oriented right.

I’m not a fan of getters/setters. And there’s language that makes little sense, one of the reasons I don’t even like UML anymore (and a lot of people are seeing that I don’t really like it). And there’s ideomethodology that preaches that it should not use. Even OOP says this in its purest form, although I always find an exaggeration.

The model seems to be above average. But that’s what I always say, I don’t know what the problem is, I can’t tell if it’s the right solution.

Pessoa is abstract? It should. The diagram does not show this.

Still not using the constructor the right way, seems not to have read the links that I spent in the previous question.

I don’t know if Contato It should be this way, but I am speculating on a problem I do not know. It may be the exact solution. Should address not have an independence? How is the relationship between the two structures made? If it is to persist as the relationship is managed?

I don’t know what that is Empresa. What is written in the question does not tell me anything. Or says, it does not make sense. Whoever creates has to justify why he created. Never create something you can’t justify very well. But after the comments I saw that it does not make sense at all, for two reasons: the individual cannot be a company and the model is left crooked so may not even have a company, probably did not understand what it serves inheritance. This is what I said in the previous question: learn first how to use the tools before using it.

The lack of understanding of what you’re doing will make you make a lot of mistakes in sequence, and you can make them all your life if you don’t create a foundation. In this case you need to create an object that is a person, and you already have a type that defines that, it’s Pessoa, You don’t have to invent anything else. Even though it’s abstract, and I’ve already said it should be, you can create concrete objects, through their derivatives, but store them in a type variable Pessoa, and so already has what it needs. The object will be unique to that entity and will obviously follow exclusively one of the two existing current models: PessoaJuridica or PessoaFisica.

I don’t know if the tipo should be string, but could be, would need further deepening.

Are there any missing setters? Maybe not, but there is no lack of one or more methods that allow changing the data consistently?

Behold CPF or CNPJ field type in VARCHAR or INT database?.

In the comments speaks a lot in companies, company is only legal entity. It may be just the wrong term, but as I say, if you don’t even know how to use the right names of the things you don’t know what that means, what the role of that in the model, then the mistake is imminent, look for ontology and taxinomy, can help model better if you study more deeply.

This part is still easy, people make mistakes on what comes next. And I’m curious to see this later, the comments indicate good thing.

1

Taking some of the information from the comments, it became a little clearer the needs of your modeling.

I understand that it would be interesting to have these classes:

  • Personal
  • Personal
  • Addressee
  • Person
  • Enterprise
  • Contact

With some observations that guided me in the correction of your modeling:

  • Every company, regularly incorporated, is a legal entity, but not every legal entity is a company.
  • Individual may have a Company, but Individual may not be a company.

So

  1. Empresa inherits of PessoaJuridica.
  2. Pessoa has one Empresa. Remove the inheritance between Company and Personal.
  3. PessoaFisica and PessoaJuridica inherit from Pessoa.
  4. It would move the address information from Pessoa for a class Endereco. Person has a list of Endereco.
  5. Pessoa has a list of Contato, as it is currently.

Otherwise, I would use the class constructor to receive their information, and not set. Assess if you need all get existing. If yes, you can keep them.

Over the field tipo and what he explained in the comments, he seems to belong to a class Profissional that inherits of PessoaFisica and, in the case of a company, I believe that the information belongs to Ramo (of activity) of Empresa, in which the Empresa has one Ramo. You can use this Ramo for PessoaFisica and give up the Profissional, but would need to know better the context to choose. But I leave the two options.

Finally, as I said in the comments, I believe that you can choose not to use any inheritance in this model and only use composition. But in my answer I preferred to follow the inheritance reasoning so as not to run too far from what I already had. Remember that there is no right and definitive answer to this problem, because the possible solutions are several.

I’m from the time of structured programming and now I’m returning and trying to fit in with OOP. I made a diagram below, could analyze if I am on the right path?

Just one comment on that. Much is heard about OOP being the evolution of structured programming, but it all depends on the problem it will solve. It is true that formerly Macaroni codes were common and OOP was sold as salvation, but it is possible to maintain a well-organized non-OOP code. In fact, in years of work, it’s the most I’ve seen to date.

In fact, applying OOP to an application is quite complicated and is usually recommended for cases where the business rule is quite complicated, in which commonly the DDD (Domain Driven Design) appears to give a certain organization to all these classes that can be repeated in different domains of the same application.

0

One of the principles that govern object orientation is that classes possess characteristics and have behaviors that are inherent to them and translate what they represent.

In your class Person you have placed attributes such as neighborhood, zip code, city and state that are not characteristic of a person but rather of the address associated with it. Still, as I understand it, what you used as attribute named address, would be the street + number. These, together with the other attributes mentioned above, are associated with the set that characterizes the address of a person. It would be the same line of reasoning that you used to create a Contact class with the attributes inherent to it.

I advise you to put address as a class and bring all address data to be attributes of this new class Addressee. Then make a relationship between Person and Address just like you did with Contact.

Making these changes I believe that your model will translate into extensible code more correctly and allow reuse more efficiently.

  • 2

    And why should they follow this advice? What are the advantages and benefits? What are the problems with the current structure that benefits this change? Could you describe this in your answer?

  • Anderson, really my answer was very loose. Thanks for the feedback. I added information regarding the principles of object orientation.

Browser other questions tagged

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