Search code examples
oopumldiagramclass-diagram

How to model the relationship between two classes that are linked with the same attribute in UML Class Diagrams?


I have the two following classes that are linked only through a common attribute:

Two classes linked only through a common attribute

How to model the relationship between these classes, knowing that testID in ErrorMessage and id in Test are the same (meaning they represent the same value)

What UML-relationship best describes this case?


Solution

  • If Test::id is an attribute that uniquely identifies a Test, the relationship would be an association with the multiplicity 1 on the side of Test (i.e. each ErrorMessage instance is associated with one Test). As nothing prevents several different ErrorMessage instances to refer to the same TestId, the multiplicity would be * on the side ErrorMessage. Note that you could add the property modifier {id} after the id attribute to clarify that the attribute is part of the identifier for the class.

    Now it seems that the ErrorMessage lifecycle is related to the Test instance: to create an ErrorMessage, you need to have the id. Conversely, as the id is not optional (multiplicity 0..1), we can assume that it cannot exist without a Test instance. You may then use a composite aggregation, putting a black diamond on the side of test.

    This being said, usually when designing classes, the attributes needed to implement associations are not shown. This avoids ambiguities, such as: is this int an additional attribute on top of the documented association or is it just its implementation? (you know, but not other readers). Alternatively, when modelling a comprehensive database schema ("physical model") often some specialized UML profiles are used, with stereotypes that help to clarify primary and foreign keys.