You use an association to model how objects of two classes relate to each other; use it when one object needs to refer to one or more objects of another class.
For example, an Invoice can be related to a Customer. The association records that relationship in the model, rather than storing a list of customers or invoices in an attribute.
Contents
When to use an association
Use an association when the information is another object in your model, or a collection of other objects.
| Need | Model it as | Example |
|---|---|---|
| A single simple value | An attribute | An Invoice has an invoice number.
|
| A relationship to another object or a list of objects | An association | An Invoice is related to a Customer; a Customer can be related to invoices.
|
Attributes hold simple, single values. To handle lists of values, use associations.
Association ends
An association has two ends: one end for each participating class. Give each end a meaningful name that explains the role of the related object.
For the Invoice and Customer example, names such as Customer and Invoices make the relationship understandable from both classes.
The package naming convention can help produce useful default names. If a package has a plural s suffix, associations to a class such as Book may receive the default name Books.
Create or change an association
- Identify the two classes that participate in the relationship. Create the class first if it does not yet exist.
- Add or edit the association between those classes.
- Name both association ends according to the role they represent.
- Check the relationship in a diagram. A diagram can show the association without changing the underlying model.
- Save the model and run validation. The model must be clean, with no errors, before you start the Prototyper or deploy it.
For the detailed association-editing task, continue to add a single association.
Relate a class to itself
Both ends of an association can belong to the same class. Use this when objects of one class have different roles in relation to other objects of that same class.
For example, create an association from Person to Person when a person can be a teacher for other people. Name the ends Teacher and Pupil so that each role is clear.
Keep associations understandable
- Use names that describe the role at each end, especially when the same class appears at both ends.
- Put related classes together in a diagram that documents the part of the model you are working on. A class can appear in more than one diagram.
- Keep simple data, such as names and numbers, as attributes; use associations for object relationships.
- Validate after changes so that model errors are found before prototyping or deployment.
