Top Banner
1 UML Sequence Models
27

UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

Mar 10, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

1

UML Sequence Models

Page 2: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

2

Specifying behavior using the UML

� Class models describe objects and their relationships� Behavior can be specified in terms of operation pre and

postconditions, but behavior is not the primary focus of a class model

� Behavioral models in the UML� State models: describe control aspects of a system –

provides descriptions sequences of operations without regard for what the operation do.

� Interaction models: describe interactions among objects� Activity models: description of a behavioral feature

expressed in terms of sequences of steps.

Page 3: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

3

How things happen in the UML

� An action is executed by an object� May change the contents of one or more variables or slots

� If it is a communication (“messaging”) action, it may:

� Invoke an operation on another object

� Send a signal to another object

� Either one will eventually cause the execution of a procedure onthe target object…

� …which will cause other actions to be executed, etc.

� Successor actions are executed

� Determined either by control flow or data flow

Page 4: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

4

Sequence Models

Page 5: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

5

Overview

� Realizations of use cases can be expressed as interaction diagrams� Objects interact to accomplish use case goals.

� Object interactions are described in terms of messages sent among objects

� Sequence diagrams allow one to view only the parts of a system involved in accomplishing use case goals

Page 6: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

6

Sequence Diagram: Basic constructs

name : Classobject symbol

lifeline

activation

other

stimulus

name (…)

return

: Class

create

new (…)

delete

Page 7: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

7

Different kinds of arrows

Synchronous message: Operation call

Asynchronous message

Return

Page 8: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

8

Example: different arrows

caller exchange callee

Asynchronous Flow

lift receiver

dial tone

dial digit

dial digit

ringing tone ringing signal

lift receiver

teller : Order : Article

Synchronous Flow

getValue

price

getName

Page 9: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

9

sd GoHomeSetup

:ServiceUser:ServiceBase

ref SB_GoHomeSetup:ServiceTerminal

opt

ref FindLocation

SetHome

SetInvocationTime

SetTransportPreferences

refAuthorization

A more complex Sequence Diagram

(UML 2.0)Frame and Name

Lifeline is an object

Interaction Occurrence

Combined Fragment

Plain asynchronous message

sd Authorization

:ServiceUser:ServiceBase

ref SB_Authorization:ServiceTerminal

Code

OK

OnWeb

OK

Page 10: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

10

Combined fragment types� Alternatives (alt)

� choice of behaviors – at most one will execute� depends on the value of the guard (“else” guard supported)

� Option (opt)� Special case of alternative

� Loop (loop)� Optional guard: [<min>, <max>, <Boolean-expression>]� No guard means no specified limit

� Break (break)� Represents an alternative that is executed instead of the

remainder of the fragment (like a break in a loop)� Parallel (par)

� Concurrent (interleaved) sub-scenarios� Negative (neg)

� Identifies sequences that must not occur

Page 11: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

11

Combined fragments and data

:ServiceUser :ServiceBase :ServiceTerminal

sd GoHomeInvocation(Time invoc)

:Clock

InvocationTime FindLocation

TransportSchedule

loop

alt

ScheduleIntervalElapsedFindLocation

TransportSchedule

GetTransportSchedule

TransportSchedule

FetchSchedule

[Now>interv+last]

[pos-lastpos>dist]

[Now>invoc]loop

Choice

Operand Separator

Guarding InteractionOperand with an InteractionConstraint

Page 12: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

12

Exampe: A Problem

� [Variation of a program example used in the book “Refactoring” by Martin Fowler]

� Program calculates and prints a statement of a customer’s charges at a video store. There are three types of movies: Regular, NewRelease, Children.

� Rates� Regular: 2.00 for 2 days; late 1.50/late day� Children: 1.50 for 3 days; late 1.50/late day� NewRelease: 3.00/day

Page 13: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

13

Design 1: Class DiagramCustomer

Rental Movie

makes

refers_to

_rentals

_movie

1

*

*

1

name: String

addRental()getName()statement()- getAmount()

daysrented:int

getDaysRented()getMovie()

title:StringpriceCode:int

getPriceCode()setPriceCode()getTitle()

Page 14: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

14

Design 1: Sequence Model

Page 15: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

15

Design 1: Code for MoviePublic class Movie{

public static final int CHILDREN = 2;public static final int REGULAR = 0;public static final int NEW = 1;

private string _title;private int _priceCode;

public Movie (String title, int priceCode){_title = title; _priceCode=priceCode;}

public int getPriceCode(){return _priceCode;}

public int getTitle(){return _title;}

public void setPriceCode(int arg){_priceCode=arg;}

}

Page 16: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

16

Code for Rental

class Rental{

private Movie _movie;

private int _daysRented;

public Rental (Movie movie, int daysRented){

_movie=movie; _daysRented=daysRented;}

public int getDaysRented(){

return _daysRented;}

public Movie getMovie(){

return _movie;}

}

Page 17: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

17

Code for Customerclass Customer{

private string _name;

private vector _rentals = new Vector();

public Customer (String name){_name = name;}

public void addRental(Rental arg){

_rentals.addElement(arg);}

public String getName(){

return _name;}

public String statement(){…}

}

Page 18: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

18

Code Skeleton for statement()

Derived from Sequence Model

public String Statement (){Enumeration rentals = _rentals.elements();

\…

while(rentals.hasMoreElements()){

\\...Rental R =(Rental)rentals.nextElement();

\\

Movie M=aRental.getMovie();

int PC=M.getPriceCode();String T = M.getTitle();

int days=R.getDaysRented(); }

\\…

}

Page 19: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

19

Code for statement()public String Statement (){

double totalAmt = 0;

Enumeration rentals = _rentals.elements();

String result = “Record for”+getName()+”\n”;

while(rentals.hasMoreElements()){Rental aRental=(Rental)rentals.nextElement();

thisAmt=getAmount(aRental);

String t=aRental.getMovie().getTitle();

result += ”\t”+ t + String.valueOf(thisAmt)+”\n”;totalAmt=totalAmt+thisAmt;

}

result+=”Amount owed is”+String.valueOf(totalAmt)+” \n”;

return result;}

Page 20: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

20

Code for getAmount()Private double getAmount (Rental aRental){

Movie aMovie=aRental.getMovie();

int pc=aMovie.getPriceCode();

int days=aRental.getDaysRented();int thisAmt = 0;

switch (pc){

case Movie.REGULAR:

thisAmt = 2;

if (days > 2)

thisAmt += (days-2)*1.5;

break;

case Movie.NEW:thisAmt = days*3;

break;

case Movie.CHILDREN:

thisAmt = 1.5;

if (days > 3)

thisAmt += (days-3)*1.5;

break;

}return thisAmt;

}

Page 21: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

21

Design 2 Sequence Model

Page 22: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

22

Design 2 Class Diagram

Customer

Rental Movie

makes

refers_to

_rentals

_movie

1

*

*

1

name: String

addRental()getName()statement()getTotalCharge()

daysRented:int

getDaysRented()getMovie()getCharge()

title:StringpriceCode:int

getPriceCode()setPriceCode()getTitle()

Page 23: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

23

Design 3: Class Diagram

Rental Movie

makes

refers_to

_rentals

_movie

1

*

*

1

Customer

name: String

addRental()getName()statement()getTotalCharge()

daysRented:int

getDaysRented()getMovie()getCharge()

title:String

getCharge()getTitle()

Price

getCharge(days)

NewRelPrice

getCharge(days)

ChildrenPrice

getCharge(days)

RegularPrice

getCharge(days)

1

*

has_state

Page 24: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

24

Design 3: Sequence Model

Page 25: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

25

sdsd ATMATM--transactiontransaction

client: atm: dbase:

Referencing interaction diagrams

insertCard

CheckPinref

alt [chk= OK]

[else]error(badPIN)

DoTransactionref

sdsd CheckPinCheckPin

client: atm: dbase:

askForPIN

data(PIN)check(PIN)

result(chk)result(chk)

Interaction Frame Lifeline is one

object or a part

Lifeline is one

object or a part

Interaction OccurrenceInteraction Occurrence

Combined (in-line)

Fragment

Combined (in-line)

Fragment

Page 26: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

26

Creating Interaction Model

� Set interaction context� Use Case scenario

� Identify (controller) object responsible for handling the event initiating the interaction

� Identify objects that collaborate with the controller (collaborators).

� Specify message passing sequence that handles the initiating message.

Page 27: UML Sequence Models - Colorado State Universityfrance/CS314/Lectures/2007-Lectures/Sequence... · Specifying behavior using the UML Class models describe objects and their relationships

27

Interaction modeling tips

� Set the context for the interaction.� Include only those attributes of the objects that are relevant.� Express the flow from left to right and from top to bottom.� Put active objects to the left/top and passive ones to the

right/bottom.� Use sequence diagrams

� to show the explicit ordering between the stimuli

� when modeling real-time