Sequence Diagrams

advertisement
Sequence Diagrams
Interaction Diagrams
1. Interaction Diagrams – Used to model the dynamic aspects of a software system. They help you to visualize how
the system runs. At the design stage they are useful for refining a class diagram, e.g. are the methods in the
correct class?, are the parameters correct, missing?, navigability, etc.
2. There are two types of Interaction Diagram: Sequence Diagrams and Collaboration Diagrams. They are virtually
identical.
3. Interaction diagrams show how a set of actors and objects communicate with each other to perform the steps of
a use case, or the steps of some other piece of functionality.
4. You can use interaction diagrams to show how a use case works, a method works, or any piece of functionality.
5. Interaction diagrams can show several different types of communication.
 E.g. method calls, messages sent over the network
 These are all referred to as messages.
Section 8.1 – Sequence Diagrams
1. A Sequence Diagram shows the messages that are sent when the system is accomplishing a task.
2. Example 1 – Consider the use case and class diagram shown below to draw the corresponding sequence
diagram.
Use case: A request is made to register a student.
Class diagram:
Sequence diagram:
1
3. Anatomy of a Sequence Diagram – A sequence diagram shows the sequence of messages exchanged by the set
of objects performing a certain task
a. Instances of classes – Shown as boxes with the class and object identifier underlined
b. The objects are arranged horizontally across the diagram.
c. An actor that initiates the interaction is often shown on the left. Use the stick-person symbol as in use case
diagrams
d. The vertical dimension represents time.
e. A vertical line, called a lifeline, is attached to each object or actor.
f. The lifeline becomes a broad box, called an activation box during the live activation period.
g. A message is represented as an arrow between activation boxes of the sender and receiver.
 A message is labelled and can have an argument list and a return value.
 The recipient of the message is the owner of the message.
4. Conditional behavior in a sequence diagram is indicated by putting a box around the conditional code as shown
in the example below. The condition for entering is shown in brackets. An [else] block can be added.
5. Example 2 – Consider the use case and class diagram shown below to draw the corresponding sequence
diagram.
Use case: A request is made to register a student. Note: student must have passed the prerequisite course.
Class diagram: Note: we’ve added two methods, getPrerequisite in the Course class and hasPassed in the
Student class.
Sequence diagram:
2
6. Iteration in a sequence diagram is indicated by placing a box around code to be repeated and specifying how the
iteration occurs.
7. Example 3 – Consider the use case and class diagram shown below to draw the corresponding sequence
diagram.
Class diagram: A Bill contains a number of LineItems where each LineItem corresponds to one Item
(Product). A LineItem specifies how many of the Item we want (and possibly other things).
Use case: ObtainBill – Obtain the total bill.
Sequence diagram:
8. Object deletion in a sequence diagram is indicated by placing an “X” where the deletion occurs.
9. Example 4 – Consider the use case and class diagram shown below to draw the corresponding sequence
diagram.
Class diagram: This is the same situation as Example 1, except that we have added 4 methods:
cancelRegistration and deleteFromRegistrationList in the CourseSection class, deleteFromSchedule in the
Student class, and cancel in the Registration class.
Use case: CancelRegistration – Cancel the registration for a course.
3
Sequence diagram:
Sequence Diagram Examples
1. A sequence diagram can be ambiguous at times, without the indication of when a method finishes. Thus, we
should add return arrows (dashed arrow with method name) when necessary. An example is shown below.
2. Example 7 – Consider the following code to draw the corresponding sequence and collaboration diagrams for
the situation where we are starting the system.
public class A {
public static void main(String[] args) {
B b = new B();
b.launch();
}
}
class B {
public B(){}
public void launch() {
System.out.println("B being launched");
}
}
4
Sequence Diagram for Starting System
Sequence Diagram with returns
3. Example 8 – Consider the following code to draw the corresponding class, sequence, and collaboration diagrams
for the situation where we are starting the system.
public class A2 {
public static void main(String[] args) {
B b = new B();
b.launch();
}
}
class B {
C c;
public B() {
c = new C();
}
public void launch() {
System.out.println("B being launched");
init();
}
private void init() {
System.out.println("B being init'd");
}
}
class C {
public C() {
setup();
}
public void setup() {
System.out.println("C setup");
}
}
5
Class Diagram:
Sequence Diagram for Starting the System
6
4. Example 11 – This sequence diagram is ambiguous. Is m3 called from m or is m3 called from m2?
a. m3 called from m:
b. m3 called from m2:
7
5. Example 12 – Consider the following code:
import java.util.*;
class D {
public class A7 {
public static void main(String[] args) {
C c = new C();
c.m1();
}
}
public D() { go(); }
public void init() { go(); }
public void go() { }
public void calc(C c) {
int z = c.getVal();
}
}
class B {
C c;
int z;
class E {
public void setSpeed() {}
public void execute() {}
public void reset() {}
public B(){}
}
}
class C {
ArrayList<D> dCol;
E e;
boolean isReady;
public C() {
System.out.println("create C");
isReady = Math.random() < 0.2 ? true : false;
dCol = new ArrayList<D>();
for( int i=0; i<9; i++ ) {
D d = new D();
dCol.add(d);
}
e = new E();
}
public void m1() {
System.out.println("m1()");
for( D d : dCol ) {
d.calc(this);
}
if ( isReady )
e.setSpeed();
else {
for( D d : dCol ) {
d.init();
}
e.reset();
}
e.execute();
}
public int getVal() {
return (int)Math.random();
}
}
8
a. Draw the corresponding class diagram
b. Draw a complete sequence diagram for the situation where a C object is created.
9
c. Draw a sequence diagram for the situation where m1 in the C class is called.
Blank Template for Sequence Diagram (next page)
10
11
Homework
1. Rework appropriate examples in the notes above:
a. Consider the code provided.
I. Draw the class diagram
II. Draw a sequence diagram for starting the system.
b. Consider the sequence diagram provided.
I. Write the corresponding code.
2. Briefly discuss what a Sequence diagram is used for.
3. Consider this class diagram that relates to the genealogy example. Draw a sequence and
collaboration diagram for each of these methods that will be added to the Person class: (a)
getParents(),
(b)
getMyChildren(),
(c)
getMySiblings(),
(d)
getMyCousins(),
(e)
getMyGrandparents(), (f) getMyAuntsAndUncles()*
* Solution provided for part f at the end of this document.
4. Consider the pseudo-code below (next page). (a) Draw a class diagram, (b) Draw a complete
sequence diagram for starting the system
12
class A
D d
C c
int dim
class C
A a
int x
C( A a, F f )
this.a = a
x = f.getCoord()
a.setDim( x )
static void main()
A a = new A()
A()
d = new D()
F f = new F( d.getParam() )
c = new C( this, f )
void updateModel( int y )
x = a.getData( y )
void setDim( int x )
dim = x + 1
int getData( int y )
F f = new F( y * d.getParam() )
return f.getCoord()
class D
int soln
class F
int param
D()
F( int p )
param = p
setSolution()
void setSolution()
soln = 1.24523
int getCoord()
return param*
int getParam()
return soln/2
5. There are two classes: Employee and Company. The Company class has methods: moveEmployee,
getEmployee, getNumEmployees, addEmployee. The moveEmployee accepts an Employee object
and another Company object. It searches for the Employee and if it is found, it is added to the
other Company. Pseudo-code for the moveEmployee method is shown below. Draw a sequence
diagram for this method.
moveEmployee( x:Employee, newCopany:Company )
i=0
while( moreEmployees and notFound )
Employee e = getEmployee(i++)
if( e.compareTo(x) == 0 )
newCompany.add(e)
notFound = false
else if( i >= getNumEmployees() )
moreEmployees = false
13
Solution, Problem 3, part f
Assuming we can use the methods from parts a-e:
or this will get just the “blood” aunts and uncles:
14
or, using just the methods provided in the class diagram:
15
Download