0

Suppose I have the following problem:

I have Users who work in Departments. A user can work in multiple departments and a department can have multiple users. Each departament gives to its users only one role.class-diagram

Could someone review this design?

  • 2
    @FelipeEndlich you're not looking at lazy answers. You're looking at the selfless efforts of respected members of this community who are pointing you to wisdom that will help you construct a better received question next time. That is, if you listen to them. – candied_orange Jun 6 '19 at 13:23
  • 1
    Possible duplicate of Choosing the right Design Pattern – BobDalgleish Jun 6 '19 at 18:07
  • Introduce an association class between the users and departments. Your diagram has users and departments, but it doesn't show roles. A Role would play a role of an association class [no pun intended]. – Nick Alexeev Jun 6 '19 at 21:53
4

You're using subclassing when instances will work fine and actually better — the users should be instances of User (or instances of an more general Person even better) rather than subclasses of User, and, let's also assume that the departments should be instances of Department rather than subclasses of Department.

People's roles change but they stay the same person.

The association between user and department is just that, an association, with 1:N mappings between User and Department.


You should only subclass if you need to:

  1. add additional data members but that only apply to one kind of department or user — meaning that you cannot add them to the base class as that would be somehow inappropriate for others

  2. provide overriding behaviors that differentiate one department or user from another.

If you can capture the concept of different users and different departments by having a different Id or Name in the base class, then all you need are instances not subclasses.


Further, if you need to dynamically add a user or a department, instances work great as you can do that at runtime but subclasses don't, so you'd have to revise the program for each such addition.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.