You are on page 1of 7

Lecture No.

12
Sequence Diagram

A sequence diagram is an interaction diagram that emphasizes the time ordering of


messages. It shows a set of objects and the messages sent and received by those
objects. The communication protocol which object follow while communicating with
another object can be shown by sequence but the important point is that sequence
diagram does not capture a particular instance (only one) communication point
rather it show object interaction till it is active or alive in the memory. It is used
primarily to design, document and validate the architecture, interfaces and logic of
the system by describing the sequence of actions that need to be performed to
complete a task or scenario. Sequence diagram is a very useful design tools
because it provide a dynamic view of the system behavior which can be difficult to
extract from static diagrams or specifications.

Notation:
i. Object:

Graphically, a sequence diagram is a table (2 dimensions) that shows objects


arranged along the X axis (horizontal) and is represented by box with a dashed line
descending from it. The line is called the object lifeline, and it represents the
existence of an object over a period of time, as shown in Figure below.

Object name can be represented by Class name: Instance Name, for example
Book b but for the sake of simplicity usually class name is optional. If the object is
created or destroyed during the period of time then its lifeline starts or stops at the
appropriate point; otherwise it goes from the top to the bottom of the diagram. If
the object is destroyed during the diagram, then its destruction is marked by a large
X.
Activation of an Object:

Life line of the object represents the existence of an object in the memory but this
does not mean that during the existence of an object it is in Activation mode also
i-e interacting with other objects also. Activation (focus of control) shows the period
of time during which an object is performing an action either directly or through a
message. Activation is shown as a tall thin rectangle whose top is aligned with its
initiation time and whose bottom is aligned with its completion time as shown
below:

ii. Messages

Messages are shown along Y-axis (Vertical) and are ordered in increasing time.
Messages are shown by directed arrow from caller (Object 1) to calling (Object 2)
instance as shown below:
Validate data is the method which exists in class 2 (Object 2) and is accessed by
object of class 1 (Object 1), if validate method is returning any value then it is
represented by a directed arrow with dashed line as shown in the bottom of the
above. There are three types of messages:

i. Synchronous

In this type of message, the object that calls a method or sends a message is
blocked or

waits for the response from the called object. During the request activation timeline
the

calling object is in wait state to perform any other task. Synchronous message call is

shown by a directed filled arrow with dashed line as shown below:

ii. Asynchronous
This type of message is opposite to that of Synchronous message call, in this type of
message the flow is not interrupted and response is not awaited i-e caller is not
blocked till response and caller can perform any other activity. An asynchronous
message is drawn with a half-arrowhead, that (one with only one wing instead of
two) as shown below:

iii. Recursive

Recursive message is like recursion concept in programming language in which


there is communication between same message i-e calling and responding message
are same. It is like recursion in which within the body of method there is call to
itself. The message starts and finish at the same message as shown below:

Guards in Sequence Diagram

Till now we have discussed the basics of sequence diagram in which communication
between objects is condition less but in real-time when modeling object
interactions, there will be times when a condition must be met for a message to be
sent to the object. There will be certain prerequisite for communication or a
message to be sent to the sender. These conditions are attributed as Guard in
sequence diagram, a guard behaves likes if statements in the sequence diagram.
They are used to control the flow of the messages between objects. Guard (bars)
and those elements above the message line being guarded and in front of the
message name as shown in the sequence diagram below:

The communication between Register office and drama will take place only when
the past dues of a student is clear and there are no pending dues, this process is
verified by AccountsReceivable object of ar class. [PastDueBalance=0] is a guard (if
statement) which is placed above the message line before the call to addstudent
method of drama class.

Combined Fragments:

Combined fragment is an interaction fragment which defines a combination


(expression) of interaction fragments. A combined fragment is defined by an
interaction operator and corresponding interaction operands, for every option there
will be corresponding action. In most sequence diagrams, guard is not sufficient to
handle the logic required for a sequence being modeled. A combined fragment is
used to group sets of messages together to show conditional flow in a sequence
diagram. We need to show a complete set of sequence of communication flow
within sequence diagram. There are three combined fragments which are discussed
below:

ALT:

Alt keyword represents alternatives to designate a mutually exclusive choice


between two or more message sequences. Alternatives are used to simulate if then
else logic as it occur in the programming languages. Guards are used to simulate
Alternatives in Sequence diagram. These guards provide the complete alternate
flow within the sequence diagram for communication between objects.

OPT (Options) Guard:

The option guard is used to model a situation that, given a certain condition, will
occur; otherwise, the sequence does not occur. An option is used to model a simple
if then statement. The option combination fragment notation is similar to the
alternative combination fragment, except that it only has one operand and there
never can be an else guard. To show an option guard, we draw a frame and text
opt is placed inside the frame's name box and guard is placed at top left corner in
the frame's content area. The option's sequence of messages is placed in the
remainder of the frame's content area as shown below:
Loop:

Occasionally you may need to model a repetitive sequence. The loop combination
fragment is used to model repetitive sequence. The loop combination fragment is
similar to the option combination fragment, except that the text loop is placed in
frame's name box. A guard is placed at top left corner inside the frame's content
area . Then the loop's sequence of messages is placed in the remainder of the
frame's content area as shown below:

You might also like