Reputation: 642
CONTEXT: I have an abstract class Student. From this class I derive three concrete classes SchoolStudent, UnderGradStudent, PostGradStudent. I have another class Vehicle. A school student must NOT drive a vehicle however, an undergrad or a postgrad student may or may not drive a vehicle.But every vehicle must be driven by somebody hence the Vehicle class has a reference to a student.
PROBLEM: I have association between classes UnderGradStudent & Vehicle and another association between PostGradStudent & Vehicle. However, I am a little confused about the multiplicity.
As I understand the problem, the UML class diagram should look something like this:
However, I suspect the above diagram suggests that each Vehicle will have one UnderGradStudent as well as one PostGradStudent.
Is the above diagram correct as to what I want to model corresponding to the context?
Upvotes: 3
Views: 916
Reputation: 6529
As you've drawn this UML diagram, it implies (but does not say correctly) that a Vehicle
must be driven by both one UnderGradStudent
and one PostGradStudent
. It also implies that an UnderGradStudent
can drive any number of Vehicle
s and a PostGradStudent
can drive any number of Vehicle
s (at the same time). I don't think that's what you intended. The reason I say "implies" is that you have overlaid two associations on one end. Last I checked, that is invalid UML.
I think you wanted to say that a Vehicle
may be driven by up to one Undergrad Student
or Postgrad Student
. To say that, I recommend the following model:
What this says is:
Student
must be one of School Student
, Undergrad Student
, or Postgrad Student
(and not multiply classified)Allowed Driver
must be one of Undergrad Student
or Postgrad Student
(and not multiply classified)Vehicle
can be driven by up to one Allowed Driver
(at a time)Allowed Driver
drives up to one Vehicle
(at a time)This describes a valid situation at any point in time, which is really useful. Think of it as a way to evaluate the validity of any frame in a movie.
If you need to record all the drivers of every vehicle ever, you would need to make many changes. A Person
actually plays the role of a Student
(among other roles, usually), and that role can change over time. You would need to record the start and end time of every role change and the start and end time of every driver / vehicle change. Think of this as recording all the frames in a movie, but without the ability to express the validity of any given frame in the movie. You lose that ability when you relax multiplicities.
You can get the benefits of the "single frame" and the "whole movie" approach by combining all of the associations I mentioned.
Upvotes: 3
Reputation: 36295
Your problem will easily be solved by using a 0..1 multiplicity near the *Student
classes from the Vehicle
. This will tell the reader that both are allowed to have a related optional vehicle. To avoid both using the same car you need to attach a constraint like this:
Alternatively you can do it the following way:
I'm not good in writing OCL but you could formalize the constraint as well.
Upvotes: 2