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.
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.
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.