16.4. Ways to Show UML Attributes: Attribute Text and Association Lines
Attributes of a classifier (also called structural properties in the UML259).
Figure 16.3 shows these notations being used to indicate that a Register object has an attribute (a reference to) one Sale object.
Figure 16.3. Attribute text versus association line notation for a UML attribute.
[View full size image]
visibility name : type multiplicity = default {property-string}
Also, the UML allows any other programming language syntax to be used for the attribute declaration, as long as the reader or tool are notified.As indicated in Figure 16.1, visibility marks include + (public), - (private), and so forth.Guideline :
Attributes are usually assumed private if no visibility is given.Notice in multiplicity notation described on p. 153
Guideline :
When showing attributes-as-associations, follow this style in DCDs, which is suggested by the UML specification. It is true that the UML metamodel also allows multiplicity and rolenames at the source end (e.g., the Register end in Figure 16.3), and also an association name, but they are not usually useful in the context of a DCD.Guideline :
On the other hand, when using class diagrams for a domain model do show association names but avoid navigation arrows, as a domain model is not a software perspective. See Figure 16.4.
Figure 16.4. Idioms in association notation usage in different perspectives.
associations explored while applying class diagrams to domain modeling, on p. 149. This is an elaboration of the notation for use in the context of a software perspective DCD.
Guideline: When to Use Attribute Text versus Association Lines for Attributes?
This question was first explored in the context of domain modeling on p. 164. To review, a data type refers to objects for which unique identity is not important. Common data types are primitive-oriented types such as:
- Boolean, Date (or DateTime), Number, Character, String (Text), Time, Address, Color, Geometrics (Point, Rectangle), Phone Number, Social Security Number, Universal Product Code (UPC), SKU, ZIP or postal codes, enumerated types
Guideline :
Use the attribute text notation for data type objects and the association line notation for others. Both are semantically equal, but showing an association line to another class box in the diagram (as in Figure 16.3) gives visual emphasis it catches the eye, emphasizing the connection between the class of objects on the diagram. See Figure 16.5 for contrasting examples.
Figure 16.5. Applying the guidelines to show attributes in two notations.
[View full size image]
public class Register
{
private int id;
private Sale currentSale;
private Store location;
// …
}
The UML Notation for an Association End
As discussed, the end of an association can have a navigability arrow. It can also include an optional rolename (officially, an association end name ) to indicate the attribute name. And of course, the association end may also show a multiplicity value, as explored earlier on p. 153, such as '*' or '0..1'. Notice in Figure 16.3 that the rolename currentSale is used to indicate the attribute name.And as shown in Figure 16.6, a property string such as {ordered} or {ordered, List} is possible. {ordered} is a UML-defined keyword that implies the elements of the collection are (the suspense builds…) ordered. Another related keyword is {unique} , implying a set of unique elements.
Figure 16.6. Two ways to show a collection attribute in the UML.
[View full size image]
How to Show Collection Attributes with Attribute Text and Association Lines?
Suppose that a Sale software object holds a List (an interface for a kind of collection) of many SalesLineItem objects. For example, in Java:
Figure 16.6 shows two ways to illustrate a collection attribute in class diagrams.Notice also the optional use of property strings such as {ordered} .
public class Sale
{
private List<SalesLineItem> lineItems =
new ArrayList<SalesLineItem>();
// …
}