Getting Dynamic: Java and UML Interaction Diagrams

Interaction diagrams depict a specific set of interactions between a set of objects. In this fourth article introducing UML from a programmer's perspective, Stephen Palmer compares UML sequence and collaboration diagrams with equivalent Java source code constructs.

Like this article? We recommend

Welcome to the fourth article in a series introducing UML and object modeling
from a Java programmer's perspective. In the previous articles, we
introduced UML class diagramscomparing their contents to classes and
interfaces in the Java programming language. In this article, we move on to
consider another type of UML diagram: the interaction diagram. Interaction
diagrams depict a specific set of interactions between a set of objects. We are
essentially looking to answer the question, "What does a method call look
like in UML?"

We will be using a very simplistic and partially complete sales and tracking
system to illustrate sequence and collaboration diagrams. The system consists of
six Java classes with the following major methods, in addition to the usual
accessor methods for properties and collections:

Sale

calcPaymentsTotal the amount of all payments made for the sale

calcTotalTotal the cost of all items purchased as part of the sale

completeMark the sale transaction as finished

LineItem

calcTotalTotal the cost of one type of item purchased

Product

calcTotalTotal the cost of one type of item purchased

Payment

CreditCardPayment

authorizeAuthorize the use of the credit card for this payment

CashPayment

calcChangeCalculate the amount of change to be returned to the
purchaser

NOTE

The bare bones of the source code for the above list is attached at the end
of the article.

From the
previous articles,
we know that a useful way to view the structure of a Java system is using UML
class diagrams. Figure 1 shows a class diagram for our sales- and
payment-tracking classes. Figure 1 also uses the color-coded class archetypes
technique described in the third article in this series, "Just Typical: UML
Stereotypes and Class Archetypes," to help communicate the intent of each
class.

Interaction Diagrams

If we trace through an execution of any Java program, we see that it contains
one or more sequences of method invocations on objects and classes. We invoke a
method on an object of a class to answer a specific question or perform a
specific action. Often, that method will invoke other methodseither on
itself, on objects of the same class, or on objects of other classes. These
methods, in turn, may invoke other methods and so on until the question is
completely answered or the requested action is completely performed (or an
exception occurs that prevents the question being answered or the action being
performed).