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.
Could someone review this design?
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.
Could someone review this design?
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:
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
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.