From the UML bible, about role:
Role: A role name explains how an object participates in the relationship. Each object needs to hold a reference to the associated object or objects. The reference is held in an attribute value within the object. When there is only one association then there is only one attribute holding a reference.
- What does this sentence mean?
- Can anyone please offer an example to explain it?
Roles:A role name explains how an object participates in the relationship.
You have two classes,
Professor
andBook
and they are associated as in the following diagram:The role gives a description of the association between Professor and Book. In this case Professor is the writer of the associated book.
Each object needs to hold a reference to the associated object or objects. The reference is held in an attribute value within the object.
For this I will use another example with a one to one multiplicity.
The diagram shows that Query builder has a Query (and vice versa). How is this association depicted in the code?
You have a class
QueryBuilder
that has an attribute of typeQuery
namedquery
. In code:And you have a class Query that has an attribute of type
QueryBuilder
namedqbuilder
In code:
The attribute (query for class QueryBuilder and qbuilder for class Query) is the
reference to the associated object
When there is only one association then there is only one attribute holding a reference
In the previous example, there was one association, so we had one attribute (field) in the class to keep the reference of the associated object.
In the following diagram
Acount
has two associations withBookItem
.So, in class Account we will have two fields, one field for each association.
Note that these associations are one to many, so the fields that we have for the associations are arrays that can keep more than one BookItems.
Here you can find a good article where I borrowed most examples for this answer.
EDIT: Explanation of the association between
Author
andBook
.The line that connects
Author
andBook
in the diagram is the visualization of the association. This is a bidirectional association, which means thatAuthor
one or moreBook
objects (the books written by the author) but alsoBook
has one or moreAuthor
objects (because book can have more than one author). An association with multiplicity many (one or more) is usually implemented with a collection or an array. ClassAuthor
has a field that can be a collection or array ofBook
. The diagram does not provide the name of this fields.The first diagram that associates
Professor
withBook
provides also the names of these fields.Professor
has a field with the nametextbook
to keep itsBook
objects.Book
has a field with the nameauthor
to keep itsAuthor
objects. The type of these fields is not provided by the diagram. The fieldtextbook
could be declared as anything from the following:or
or
or
Also the visibility of the fields is not provided (could be default, private or public).
There is a very good reason why this information is omitted from the class diagram: The author of the diagram did not consider it important for the message he wants to communicate with the diagram. We must not forget that UML diagrams are used to help understanding of a system by visualizing some of its aspects. Usually we create more than one diagrams in order to provide different perspectives of a system. In most cases the most important element of information is the relationship between the classes. So the implementation details are often omitted. Note there there are a lot of implementation details regarding the
Book
-Author
association that are omitted from these diagrams. How to force thatBook
will always have at least oneAuthor
or how to ensure that if aBook
has anAuthor
then also theAuthor
has thisBook
are among the details that are omitted.