UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory UML Weekend Crash Course™ - To Parent Directory

crnarupa.singidunum.ac.rs
from crnarupa.singidunum.ac.rs More from this publisher
01.01.2015 Views

Session 16—Modeling the Dynamic View: The Sequence Diagram 173 Reference #4 points to an asynchronous event. Typically, you see events that require some type of response like addItem (did it work or not) or productAvailable (is there any product in stock). But there are times when the event is simply a signal to another object to do something. For example, just a moment ago my wife told me that dinner was ready. Knowing my work habits, she knows better than to expect a response. In Figure 16-3, message 7, you send a message to inventory to reduce the count on the product by the quantity supplied. It is up to inventory to queue the request and take care of it. The place order process is not going to wait. (This makes a good illustration but it is probably not very good design.) Note the difference in the arrows. An asynchronous message uses a stick arrowhead instead of the solid arrowhead used for simple or synchronous messages. Now, take a look at messages 4 and 8. Message 4 starts the operation “addProduct” but the return doesn’t come until message 8. All the messages between 4 and 8 are the messages sent by the Order object while performing the operation “addProduct.” This is another good reason to show the returns. Without the returns explicitly shown in Figure 16-3, it would be possible to interpret the diagram to say that the system first adds the product before it even checks to see if the product is available. Finally, to model object creation, you have a few options. In Figure 16-3, message 1, you see the message Order() pointing to the object lifeline. This is a common coding convention for a constructor (an operation with the same name as the Class), an operation that creates an object. But the Sequence diagram uses an object lifeline that should allow us to represent object creation visually. Figure 16-4 shows two variations using the object lifeline. Bill : Customer Standard form Bill : Customer Also used in modeling tools 1: Order() 2: return order Bill's Order : Order 1: Order() 2: return order Bill's Order : Order Figure 16-4 Two ways of modeling object creation The example on the left is the form explicitly defined by the UML. The creation message (constructor) points directly at the Object. This means that the object icon has to be placed somewhere down the page where the creation actually happens instead of at the top. Using this technique implies that objects at the top of the page already existed when the scenario started. The example on the right is a minor variation where the constructor points to the object lifeline just below the object. But the object is still placed on the diagram at the point in time when it is created, rather than at the top. The goal is to represent the fact that the object did not exist prior to the creation message. The object lifeline literally comes into existence when the creation message is sent, so there is no object lifeline before (above) the creation message. Note Not all tools support either of these techniques, so you may have to use the approach in Figure 16-3.

174 Saturday Afternoon REVIEW The dynamic view includes diagrams designed to illustrate how objects work together. The Sequence and Collaboration diagrams specifically show how objects talk to one another to accomplish a goal of the system, one scenario in a Use Case, or one operation. The Sequence diagram is built around three fundamental elements: the objects, messages, and the object lifeline. The objects represent the participants. The messages represent the communication that they send to one another. The object lifelines allow us to arrange the messages in the proper relative sequence. Messages may be synchronous (requiring a response) or asynchronous (not requiring a response). A simple or synchronous message uses a solid line with a solid arrowhead. The asynchronous message uses a solid line with a stick arrowhead. Both of these message types represent the invocation of an operation on the object they are pointing to. So the name of the message becomes an operation signature with the name, arguments, and return type. The return from or answer to a message uses a dashed line and a line-style arrowhead. The return is simply information, so it is written on the return arrow. A message may be conditional on some prior result. This can be shown using the guard condition notation placed in front of the operation name. An object may be active or inactive at various times in the Sequence diagram. To show that an object is busy, use a narrow vertical rectangle, called an activation bar, on the object lifeline from the time the object becomes active to the time that it stops. An object usually becomes active because it has been asked to do something. It becomes inactive when it is finished with the current task. When an object is deleted or destroyed, the timeline ends and an X marks the termination. QUIZ YOURSELF 1. What does the dynamic view represent (See “Knowing the purpose of Sequence and Collaboration diagrams.”) 2. Where do you place the participating objects in a Sequence diagram (See “Defining the basic notation of the Sequence diagram.”) 3. How does the Sequence diagram show that one object tells another object to do something (See “Defining the basic notation of the Sequence diagram.”) 4. How does the Sequence diagram show that an object is busy (See “Defining the extended notation for the Sequence diagram.”) 5. How does the Sequence diagram show that an object no longer exists (See “Defining the extended notation for the Sequence diagram.”)

Session 16—Modeling the Dynamic View: The Sequence Diagram 173<br />

Reference #4 points to an asynchronous event. Typically, you see events that require some<br />

type of response like addItem (did it work or not) or productAvailable (is there any product<br />

in stock). But there are times when the event is simply a signal to another object to do<br />

something. For example, just a moment ago my wife told me that dinner was ready. Knowing<br />

my work habits, she knows better than to expect a response. In Figure 16-3, message 7, you<br />

send a message to inventory to reduce the count on the product by the quantity supplied. It<br />

is up to inventory to queue the request and take care of it. The place order process is not<br />

going to wait. (This makes a good illustration but it is probably not very good design.)<br />

Note the difference in the arrows. An asynchronous message uses a stick arrowhead<br />

instead of the solid arrowhead used for simple or synchronous messages.<br />

Now, take a look at messages 4 and 8. Message 4 starts the operation “addProduct” but<br />

the return doesn’t come until message 8. All the messages between 4 and 8 are the messages<br />

sent by the Order object while performing the operation “addProduct.” This is another good<br />

reason to show the returns. Without the returns explicitly shown in Figure 16-3, it would be<br />

possible to interpret the diagram to say that the system first adds the product before it<br />

even checks to see if the product is available.<br />

Finally, to model object creation, you have a few options. In Figure 16-3, message 1, you<br />

see the message Order() pointing to the object lifeline. This is a common coding convention<br />

for a constructor (an operation with the same name as the Class), an operation that creates<br />

an object. But the Sequence diagram uses an object lifeline that should allow us to represent<br />

object creation visually. Figure 16-4 shows two variations using the object lifeline.<br />

Bill : Customer<br />

Standard form<br />

Bill : Customer<br />

Also used in<br />

modeling tools<br />

1: Order()<br />

2: return order<br />

Bill's Order : Order<br />

1: Order()<br />

2: return order<br />

Bill's Order : Order<br />

Figure 16-4 Two ways of modeling object creation<br />

The example on the left is the form explicitly defined by the <strong>UML</strong>. The creation message<br />

(constructor) points directly at the Object. This means that the object icon has to be placed<br />

somewhere down the page where the creation actually happens instead of at the top. Using<br />

this technique implies that objects at the top of the page already existed when the scenario<br />

started.<br />

The example on the right is a minor variation where the constructor points to the object<br />

lifeline just below the object. But the object is still placed on the diagram at the point in<br />

time when it is created, rather than at the top.<br />

The goal is to represent the fact that the object did not exist prior to the creation message.<br />

The object lifeline literally comes into existence when the creation message is sent, so<br />

there is no object lifeline before (above) the creation message.<br />

Note<br />

Not all tools support either of these techniques, so you may have to use the<br />

approach in Figure 16-3.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!