Reading through the UML specifications I came across the XOR constraint (a UML predefined constraint). Thinking this would make a nice UML Tip nugget, I tried to dig deeper into this obscure little creature.
Lets have a look at four diagrams :
XOR (or an exclusive OR) is the only logical operator allowed in the UML, and is used on two or more discriminators (associations). It in effect is saying that only one of the two associations can exist at any one time.
Diagram 1. The first diagram shows how you would apply the XOR constraint to a UML diagram. Place the XOR in curly brackets and then link it up to the associations with dotted lines. The application of the XOR constraint makes sense in this first diagram, because a student can only attend one school at any one time. A particular student exclusively attends one or the other of these two schools. ‘Attends’ is an interesting word, however in my case, it is not saying that the student is physically inside the school grounds when it is attending. ‘Attends’ is a broader concept and does not involve the physicality of the student being somewhere. It is a bit like the word ‘Enrols’.
Diagram 2. The second diagram shows where you wouldn’t use the XOR constraint. In reality a student may apply to more than one university, so the XOR constraint would be wrong here.
Diagram 3. The third diagram shows an alternative to the first diagram. We can remove the two associations to the two schools and have only one association to the super class ‘School’. It is worth thinking about whenever you use a XOR constraint on two or more associations; should a single association with the super class replace the multiple associations? Is the XOR constraint indicative of a missing class?
Diagram 4. The fourth diagram shows a situation where you can’t generalize the associations. I guess, a true example where you would use the XOR constraint. Assuming that someone in this situation can either go to school or work (no after school jobs are allowed).