When an object-oriented application is running, various objects will be created from the classes defined or used in the system. These objects do not exist in isolation from each other. They interact among themselves to provide the functions of the application. In object-oriented terminology, objects collaborate among themselves to achieve an objective.
Before an object can collaborate with others, it has to know about their existence. One of the ways that this is done is through associations. A class is said to be associated with another class if their objects are directly related in some way.
Figure shows an example of the UML notation for an association. In this example, we have two classes, Author and Book, connected by a straight line.
An association is given a name. This is normally a verb or a preposition that reflects the relationship represented by the association. In our example, we call the association writes since an author writes books
We could show the direction of the association using the notation in Figure if we wish. If no direction is shown, the default understanding of the relationship is from the class on the left to the class on the right, or from the class on top to the class at the bottom.
At this stage, we should emphasise that an association is drawn between two classes only if there is a direct relationship between them. Sometimes, the situation is not obvious. For example, assume that we are given three classes: Borrower, Loan and Book for a library application. Our objective is to show the association between a borrower and the books he or she borrowed. This diagram is shown in Figure.
We might think that a borrower should be associated with a book. But an association should not be drawn between Borrower and Book. With our given objective, the borrower is only associated with those books that he or she has borrowed through a loan. The borrower is not associated with just any book.
Association between Objects
At this stage, it is useful to point out that the association is actually between objects, not classes. Although we show the association in a class diagram, it is the actual objects that are associated with each other.
For example, we could have a manager called John Smith and a branch identified as Katong Branch.
The object diagram in Figure shows the actual association between the manager, johnSmith (a Manager object) and the branch, katong (a Branch object).
When classes are linked together with associations, it is often possible to add another association based on those already shown in the diagram. Such an association is known as a derived association. If it is added to a class association diagram, the association name of the derived association is preceded by a slash, as shown in Figure.