01.01.2015 Views

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Session 10—The Class Diagram: Associations 109<br />

Association roles<br />

Sometimes the association name is a bit hard to determine. For example, what English word<br />

could you use for the association name between parents and children The <strong>UML</strong> provides an<br />

alternative that may be used in place of the name or along with it to help make the reason<br />

for the association as clear as possible. This alternative is called a role because it describes<br />

how an object participates in the association.<br />

For example, many employees contribute to a project. But you know from experience that<br />

they participate in different ways. Figure 10-3 shows how you can draw multiple associations<br />

and label them to differentiate the types of participation. Each role is placed at the<br />

end of the association next to the type of object that plays the role. You may use them on<br />

one, both, or neither end of each association.<br />

projectlead<br />

participates in<br />

Employee<br />

programmer<br />

participates in<br />

Project<br />

uidesigner<br />

participates in<br />

Figure 10-3 Role names on an association<br />

There is one other thing worth noting about roles and names. Role names generate code.<br />

Association names do not generate code. The role name can be used to name the attribute<br />

that holds the reference to the object that plays the role. In Figure 10-3, the Project<br />

object could have an attribute named programmer that holds a reference to an Employee<br />

object that plays the role of programmer, and another attribute called projectlead that<br />

holds reference to another Employee object that plays the role of project lead.<br />

Association constraints<br />

Constraints appear throughout the <strong>UML</strong> notation. You used them in Session 9 when you<br />

declared attributes and operations. Constraints fulfill much the same function for associations.<br />

First take a look at Figure 10-4, in which no constraints are specified.<br />

Person<br />

1..1<br />

Drives<br />

0..* Car<br />

Figure 10-4<br />

An association without constraints

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!