Team LiB
Previous Section Next Section

Object Diagrams

When describing how objects can interact, we sometimes find it helpful to sketch out a scenario of specific objects and their linkages, and for that we create an object diagram. An instance, or object, looks much the same as a class in UML notation, the main differences being that

Therefore, if we wanted to indicate that Dr. Brown, a Professor, is the advisor for three Students, we could create the object diagram shown in Figure 10-38.

Click To expand
Figure 10-38: Dr. Brown advises three students.

To reflect that a Student by the name of Joe Blow is attending two Sections this semester, one of which is also attended by a Student named Mary Green, we could create the diagram in Figure 10-39.

Click To expand
Figure 10-39: An instance diagram involving numerous objects

Associations As Attributes

Given Figure 10-40, which shows the association "a Course is offered as a Section," we see that a Course object can be related to many different Section objects, but that any one Section object can only be related to a single Course object.

Click To expand
Figure 10-40: A one-to-many association between the Course and Section classes

By way of review, what does it mean for two objects to be related? It means that they maintain "handles" on one another so that they can easily find one another to communicate and collaborate, a concept that we talked about in detail in Chapter 4. If we were to sketch out the attributes of the Course and Section classes based solely on the diagram in Figure 10-40, we'd need to allow for these handles as reference variables, as follows:

public class Section {
  // Attributes.
  private Course represents;  // A "handle" on a single related Course
                              // object.

  // etc.
}
public class Course {
  // Attributes.
  private Collection offeredAs;         // A collection of related Section
                                        // object "handles"
// etc.
}

So we see that the presence of an association between two classes A and B in a class diagram implies that class A potentially has an attribute declared to be either

  • A reference to a single instance/object of type B

  • A collection of references to many objects of type B

depending on the multiplicity involved, and vice versa. We say "potentially" because, when we get to the point of actually programming this application, we may or may not wish to code this relationship bidirectionally, even though at the analysis stage all associations are presumed to be bidirectional. We'll talk about the pros and cons of coding bidirectional relationships in Chapter 14.

Because the presence of an association line implies attributes as handles in both related classes, it's inappropriate to additionally list such attributes in the attribute compartment of the respective classes (see Figure 10-41).

Click To expand
Figure 10-41: Redundantly reflecting references as attributes is incorrect; the presence of an association implies these.
Note?/td>

This is a mistake commonly made by beginners. The biggest resultant problem with doing so arises when using the code generation capability of a CASE tool: if the attribute is listed explicitly in a class's attributes compartment, and also implied by an association, it may appear in the generated code twice, as shown in the following snippet representing code that might be generated from the erroneous UML diagram shown in Figure 10-41:

public class Course {
  Collection offeredAs;   // by virtue of an explicit attribute
  Collection offered_as;  // by virtue of the association
  // etc.
}

Team LiB
Previous Section Next Section