mypages.valdosta.edu · web viewsequence diagrams. sequence. diagrams. sequence diagrams – a...

22
Sequence Diagrams Sequence Diagrams 1. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by the set of objects performing a certain task: the steps of a use case, a method, or the steps of some other piece of functionality. They are used to model the dynamic aspects of a software system. 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. There are two types: Sequence Diagrams and Collaboration Diagrams, which are virtually identical. We will only consider the sequence diagram. Some references: http://www.uml-diagrams.org/sequence-diagrams- reference.html 2. Example – Consider this code: public class Driver { public static void main(String[] args) { A a = new A(); a.foo(); B b = new B(); b.fee(); } } public class A { public A() {} public void foo() {} } public class B { public B() {} public void fee() { fii(); } public void fii() {} } Draw a sequence diagram for the case when the system is run: 1

Upload: others

Post on 14-Mar-2020

72 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

Sequence Diagrams

Sequence Diagrams

1. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by the set of objects performing a

certain task: the steps of a use case, a method, or the steps of some other piece of functionality. They are used to model the dynamic aspects of a software system.

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.

There are two types: Sequence Diagrams and Collaboration Diagrams, which are virtually identical. We will only consider the sequence diagram.

Some references: http://www.uml-diagrams.org/sequence-diagrams-reference.html

2. Example – Consider this code:

public class Driver {public static void main(String[] args) {

A a = new A();a.foo();B b = new B();b.fee();

}}

public class A {public A() {}public void foo() {}

}

public class B {public B() {}public void fee() {

fii();}public void fii() {}

}

Draw a sequence diagram for the case when the system is run:

1

Page 2: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

3. Anatomy of a Sequence Diagram

a. Instances of classes – Shown as boxes with the class and object identifier underlinedb. The objects are arranged horizontally across the diagram. The order doesn’t matter however you probably

want to arrange them so that the diagram is not to cluttered.c. An actor that initiates the interaction is often shown on the left. Use the stick-person symbol as in use case

diagrams. The actor is not necessarily a person, it is frequently “some other object”.d. The vertical dimension represents time. e. A vertical (dashed) 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. Example – Consider this code:

public class Driver {public static void main(String[]

args) {B b = new B();A a = new A(b);a.fii();

}}

public class A {B b;public A(B b) {

this.b=b;b.fee();foo();

}public void foo()

{}public void fii()

{}}

public class B {public B() {}public void fee()

{}

}

Draw a sequence diagram for the case when the system is run:

2

Page 3: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

5. 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.

6. Example – Consider this code:

public class Driver {public static void main(String[]

args) {B b = new B(14);A a = new A(b);a.fii();

}}

public class A {B b;public A(B b) {

this.b=b;if(b.x>10) {

b.fee();foo();

}}public void foo() {}public void fii() {}

}

class B {int x;public B(int x) {

this.x=x;}public void fee() {}

}

Draw a sequence diagram for the case when the system is run:

3

Page 4: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

7. Example – Multi-part conditional

8. Iteration in a sequence diagram is indicated by placing a box around code to be repeated and specifying how the iteration occurs.

9. Example – Consider this code:

class Product {double price;public double getPrice() {

return price;}public Product(double price)

{this.price = price;

}}

public class Order {List<Product> prods = new

ArrayList<>();public Order() {}public void addProd(Product p) {

prods.add(p);}public double getTotal() {

double sum = 0.0;for(Product p : prods) {

sum += p.getPrice();}return sum;

}}

Draw a sequence diagram for the case when the getTotal is called:

4

Page 5: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

10. Example – 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 mvany of the Item we want (and possibly other things).

Use case: ObtainBill – Obtain the total bill.

Sequence diagram:

5

Page 6: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

11. Object deletion in a sequence diagram is indicated by placing an “X” where the deletion occurs.

12. Example – 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.

Sequence diagram:

6

Page 7: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

13. 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.

14. Example 7 – Consider the following code to draw the corresponding sequence diagram 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");

}}

Sequence Diagram for Starting System Sequence Diagram with returns

Blank Template for Sequence Diagram (next page)

7

Page 8: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

8

Page 9: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

Homework

1. Rework appropriate examples in the notes above:a. Consider the code provided.

I. Draw the class diagramII. 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. Example – Consider the following code to draw the corresponding (a) class diagram, (b) sequence diagram for the situation where we are starting the system. (Answer provided in Appendix below)

public class A2 {public static void main(String[] args) {

B b = new B();b.launch();

}}

public class C {public C() {

setup();}

public void setup() {System.out.println("C setup");

}}

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");

}}

4. Consider the following code: (Answer provided in Appendix below)

a. Draw the corresponding class diagram

b. Draw a complete sequence diagram for the situation where a C object is created.

c. Draw a sequence diagram for the situation where m1 in the C class is called.

9

Page 10: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

public class A7 {public static void main(String[] args) {

C c = new C();c.m1();

}}

class B {C c;int z;

public B(){}}

class D {

public D() { go(); }public void init() { go(); }public void go() { }

public void calc(C c) {int z = c.getVal();

}}

class E {public void setSpeed() {}public void execute() {}public void reset() {}

}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();

}}

10

Page 11: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

5. Consider this class diagram that relates to the genealogy example. Draw a sequence 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.

6. Consider the classes shown below. (a) Draw a class diagram, (b) Draw a complete sequence diagram for starting the system.

public class A {C c;D d;double dim;

public static void vv(String[] args) {

A a = new A();}

public A() {d = new D();F f = new F(d.getParam());c = new C(this, f);

}

public void setDim(double x) {dim = x+1;

}

public double getData(int y) {F f = new F(y*d.getParam());return f.getCoord();

}}

public class D {double soln;public D() {

setSolution();}public void setSolution() {

soln = 1.24523;}

public double getParam() {return soln/2;

}}

public class C {A a;double x;

public C(A a, F f) {this.a = a;x = f.getCoord();a.setDim(x);

}

public void updateModel( int y ) {x = a.getData(y);

}}

public class F {double param;public F(double p) {

this.param=p;}

public double getCoord() {return param;

}}

11

Page 12: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

7. 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. (Answer not provided in Appendix below)

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

12

Page 13: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

Appendix – Answers for Some Problems

1. Answers will vary2. Answers will vary

3.Class Diagram: Sequence Diagram for Starting the System

4.a. Draw the corresponding class diagram

13

Page 14: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

b. Draw a complete sequence diagram for the situation where a C object is created.

c. Draw a sequence diagram for the situation where m1 in the C class is called.

14

Page 15: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

5.a. Not doneb. Not donec. Not doned. Not donee. Not donef. Assuming we can use the methods from parts a-e:

or this will get just the “blood” aunts and uncles:

15

Page 16: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

or, using just the methods provided in the class diagram:

16

Page 17: mypages.valdosta.edu · Web viewSequence Diagrams. Sequence. Diagrams. Sequence Diagrams – A sequence diagram shows the sequence of messages exchanged by …

6.a.

b.

7. Not done

17