Associations

Some properties may be modeled as their own class. This makes sense:

  • When the class can be reused (e.g. both cars and bikes have wheels)

  • When the class has a lot of things that should be logically grouped together

Beware:

  • In most cases, the containing class will have a reference to the contained class (Car has an attribute of type Wheel, Lamp has an attribute of type Bulb, etc)

  • If the classes are equal, the attribute may be in either class (Person has an attribute leftNeighbor)

  • The attribute may also be in both classes. In this case special care has to be taken whn setting the attribute (when actually coding it). (Person has leftNeighbor and rightNeighbor, Car has attribute wheel : Wheel, Wheel has attribute onCar: Car)

Important

Navigable

An association from one class to another is said to be navigable. One way navigation goes from one class to another, while two-way navigation goes in both directions.

Example 3.5. Lamp with a bulb one-way navigable

Example 3.6. Lamp with bulb, two-way navigable

In UML, associations are usually shown with a line between the classes.

  • If the association is one-way navigable, the will be an open arrow at the class that the association is navigable to.

  • If the association is two-way navigable there are no arrows

  • Instead of showing an attribute in the attribute compartment, it may also be shown at the end of the association. Type will be omitted, since it is always the class its pointing to. In a two-way navigable association there will be names on both ends.

Example 3.7. Lamp with bulb, one-way navigable, shown as association

Example 3.8. Lamp with bulb, two-way navigable, shown as association

Example 3.9. Atom bomb and timer as two separate classes

Practice: Draw a diagram of an atom bomb and its timer, connect them with an association. Include at least one attribute and operation in each class. How many attributes does your bomb have?

Bomb has two attributes: size and timer.