Getting Dynamic: Java and UML Interaction Diagrams
- Interaction Diagrams
- Sequence Diagrams
- Collaboration Diagrams
- Conclusion
- Source Code
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 diagramscomparing 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
calcPaymentsTotal the amount of all payments made for the sale
calcTotalTotal the cost of all items purchased as part of the sale
completeMark the sale transaction as finished
LineItem
calcTotalTotal the cost of one type of item purchased
Product
calcTotalTotal the cost of one type of item purchased
Payment
CreditCardPayment
authorizeAuthorize the use of the credit card for this payment
CashPayment
calcChangeCalculate 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.
Figure 1 Class diagram showing the structure of a simply sales and payment tracking system.
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 methodseither 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).
UML interaction diagrams graphically represent sequences of method invocations, and come in two flavors: sequence diagrams and collaboration diagrams.