Andna
Andna

Reputation: 6689

UML association class - clarifying

I am reading "UML distilled" by Martin Fowler, and during reading about association classes I got this quote:

What benefit do you gain with the association class to offset the extra notation you have to

remember? The association class adds an extra constraint, in that there can be only one instance of

the association class between any two participating objects.

Then there was an example, but I want to make sure I got this right, if for example I got:

 ---------            ---------
|         |*        *|         |
| CLASS A |----------| CLASS B |
|         |     |    |         |
 ---------      |     ---------
                |
          ______|______
         |             |
         |             |
         |  CLASS C    |
         |             |
         |_____________|

then, for every distinct pair (instance of A,instance of B) there exists only one instance of class C.

So if I would take A1,A2,B1,B2-instances then for (A1,B1) (A1,B2) (A2,B1) (A2,B2) I would get 4 instances of C, nothing less, nothing more?

Upvotes: 5

Views: 1806

Answers (4)

Jarek Zelinski
Jarek Zelinski

Reputation: 177

Association in UML represented (have) logical sens (UML is not tool for database modeling!). Association describe possible logical fact. E.g. Two person A and B could be married, we can draw this as association, it is representing meaning like a "we know that exist an logical connection between person A and person B". If we know what that is, we draw class association [marriage cerificate] as materialised fact.

Upvotes: -1

Jim L.
Jim L.

Reputation: 6529

From the UML 2.5 specification:

Note that when one or more ends of the AssociationClass have isUnique=false, it is possible to have several instances associating the same set of instances of the end Classes.

Mr. Fowler may have gotten the facts wrong. There is no extra constraint, just the ability to store additional property values.

When isUnique=false, extra properties allow one to model multiple visits to the same doctor on different dates, or multiple purchases of the same products on different dates, for example.

Upvotes: 2

Gerd Wagner
Gerd Wagner

Reputation: 5673

Your reasoning is correct: if an association class does not have one or both association ends annotated with {nonunique}, then it implies the constraint that there can be only one link between the same objects (as explained by Martin Fowler).

Notice, however, that the option of non-unique association ends has only been added in UML 2 (in 2005), and Martin Fowler's book (from 2003) refers to UML 1.x.

Some examples may help. For instance, the association LandPurchase between Person and PieceOfLand could be modeld as a UML association class (with default unique association ends), since there can be only one purchase link between a person and a piece of land. The association ProductPurchase between Person and Product can only be modeld as an association class if the association end at the Product side is annotated as {nonunique} since there can be more than one purchase link between the same person and the same product (as a type). For instance, I can buy more than one Tesla Model S cars (if I would have the money).

Similarly, in the case of Appointment between Person and Doctor, since the same person can have more than one appointment with the same doctor, the association end at the Doctor side has to be annotated as {nonunique}.

Upvotes: 0

lmcanavals
lmcanavals

Reputation: 2366

That'd be correct, without any intention to mix concepts here but it's similar to Tables in a database where:

A 1-* C
B 1-* C

Where C can be seen as the result of breaking a many to many relationship between A and B.

For each row on B can only exist 1 and only 1 Row C and That Particular row (on C) can only me related to 1 row on A. Hence, for each Pair of unique rows on A and B can only exist 1 row on C or none, because the * indicates 0 or more.

Upvotes: 1

Related Questions