A sequence diagram is another type of UML diagram that is used to illustrate the order or sequence of execution events that occur between objects in an application. Sequence diagrams become extremely helpful and important especially when using compositional design. Figure 10-8 shows the sequence diagram for the simple aggregation example program given in examples 10.1 through 10.3 in the previous section.
Figure 10-8: Sequence Diagram — Simple Aggregation
Referring to figure 10-8 — The sequence diagram is read from left to right. The objects that participate in the sequence of events appear along the top of the diagram. Each object has an object lifeline. An object can be an instance of a class or an external system (actor) that participates in the event sequence.
The User initiates the event sequence by starting the program. This is done by running the TestDriver program with the java command-line tool. The TestDriver program creates an instance of class B by calling the B() constructor method. Upon the constructor call return the TestDriver program then creates an A object by calling the A() constructor method passing the reference b as an argument. The TestDriver then sends the makeContainedObjectSayHi() message to the A object. (i.e., a method call) The A object in turn sends the sayHi() message to the B object. This results in the message “Hi!” being printed to the console. After the message is printed the program terminates.
The sequence of events is a little different for the composite aggregate version of this program as figure 10-9 illustrates.
Figure 10-9: Sequence Diagram — Composite Aggregation
In the composite aggregate sequence, the TestDriver program creates the A object first. The A object then creates the B object. This is shown in message numbers 2 and 3. Messages 4 and 5 are the same here as they were in figure 10-8.
The sequence diagrams shown here are unique in that the example program is small enough to show the whole shebang in one picture. In reality, however, systems are so complex that engineers usually create sequence diagrams that focus on one piece of functionality. You’ll see an example of this in the next section.
Sequence diagrams are a type of UML diagram used to graphically illustrate a sequence of system events. Sequence event participants can be internal system objects or external actors. Sequence diagrams do a good job of illustrating the complex message passing between objects that participate in a compositional design.