Tải bản đầy đủ (.ppt) (32 trang)

Bài Giảng Phân tích thiết kế hướng đối tượng (phần 5) ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (833.27 KB, 32 trang )

Use case realization
Use case realization
Lecture 5
Lecture 5
Hoa Sen University 1
Agenda
Agenda

Notations

Dependency

Interaction and Class diagrams
Hoa Sen University 2
Interaction diagrams
Interaction diagrams

Generalization of two more specialized UML diagram types

Sequence diagrams

Communication diagrams

Illustrate how objects collaborate via messages and methods
Hoa Sen University 3
Sequence vs. Communication diagram
Sequence vs. Communication diagram

Sequence diagrams
illustrate
interactions in a


kind of fence format

Easier to see the call sequence

Large set of detailed notation
options

Communication
diagrams illustrate
interactions in a
graph or network
format

More space-efficient
Hoa Sen University 4
: A myB : B
doTwo
doOne
doThree
: A
myB : B
1: doTwo
2: doThree
doOne
Common notation: participants –
Common notation: participants –
lifeline box
lifeline box
Hoa Sen University 5
sales:

ArrayList<Sale>
:Sale s1 : Sale
lifeline box representing an
instance of an ArrayList class,
parameterized (templatized) to
hold Sale objects
lifeline box representing an
unnamed instance of class Sale
lifeline box representing a
named instance
sales[ i ] : Sale
lifeline box representing
one instance of class Sale,
selected from the sales
ArrayList <Sale> collection
x : List
«metaclass»
Font
lifeline box representing the class
Font, or more precisely, that Font is
an instance of class Class – an
instance of a metaclass
related
example
List is an interface
in UML 1.x we could not use an
interface here, but in UML 2, this (or
an abstract class) is legal
Sequence diagram notation
Sequence diagram notation


Lifeline boxes
and lifelines

Messages

Synchronous
message

Found message

Focus of
control and
execution
specification
bars
Hoa Sen University 6
: Register : Sale
doA
doB
doX
doC
doD
typical sychronous message
shown with a filled-arrow line
a found message
whose sender will not
be specified
execution specification
bar indicates focus of

control
Sequence diagram notation
Sequence diagram notation

Illustrating reply or
returns

Using the message syntax
returnVar = message(parameter).

Using a reply message line

Message to “self” or “this”
public class Register{
public void doX(){

clear();

}
public void clear(){
}
}
Hoa Sen University 7
: Register : Sale
d1 = getDate
getDate
doX
aDate
: Register
doX

clear
Sequence diagram notation
Sequence diagram notation

Creation of
instance

In implementation:
invoke the new
operator and call the
constructor

Object lifelines
and object
destruction

In language that does
not have garbage
collection
Hoa Sen University 8
: Register : Sale
makePayment(cashTendered)
: Payment
create(cashTendered)
authorize
note that newly created
objects are placed at their
creation "height"
: Sale
: Payment

create(cashTendered)

the «destroy» stereotyped
message, with the large
X and short lifeline
indicates explicit object
destruction
«destroy»
X
Sequence diagram notation
Sequence diagram notation

Diagram frames in UML sequence diagrams

Support conditional and looping construct
Hoa Sen University 9
enterItem(itemID, quantity)
: B
endSale
a UML loop
frame, with a
boolean guard
expression
description, total
makeNewSale
[ more items ]
loop
: A
Sequence diagram notation
Sequence diagram notation


Common frame operators
Hoa Sen University 10
Frame
operator
Meaning
Alt Alternative fragment for mutual exclusion
conditional logic expressed in the guards
Loop Loop fragment while guard is true
Opt Optional fragment that executes if guard is true
Par Parallel fragments that execute in parallel
Region Critical region within which only one thread can
run
Sequence diagram notation
Sequence diagram notation

Conditional
messages
public class foo{
Bar bar = new Bar();

public void m1(){
bar.xx();
if (color.equals(“red”))
bar.calculate();
bar.yy();
}
}

UML1.x style

Hoa Sen University 11
calculate
: Bar
yy
xx
[ color = red ]
opt
: Foo
[ color = red ] calculate
: Bar
yy
xx
: Foo

Mutually exclusive
conditional messages
public class A{
B b = new B();
C c = new C();
public void doX(){

if (x < 10)
b.calculate();
else
c.calculate();
}
}
Hoa Sen University 12
: B
: A

calculate
doX
: C
calculate
[ x < 10 ]
alt
[ else ]
Sequence diagram notation
Sequence diagram notation

Iteration over a collection
Hoa Sen University 13
st = getSubtotal
lineItems[i] :
SalesLineItem
t = getTotal
[ i < lineItems.size ]
loop
: Sale
This lifeline box represents one
instance from a collection of many
SalesLineItem objects.
lineItems[i] is the expression to
select one element from the
collection of many
SalesLineItems; the ‘i” value
refers to the same “i” in the guard
in the LOOP frame
an action box may contain arbitrary language
statements (in this case, incrementing ‘i’)

it is placed over the lifeline to which it applies
i++
Sequence diagram notation
Sequence diagram notation
public class Sale{
private List<salesLineItem> lineItems = new ArrayList<salesLineItem>();
public Money getTotal(){
Money total = new Money();
Money subtotal = null;
for (SalesLineItem lineItem: lineItems){
subtotal = lineItem.getSubtotal();
total.add(subtotal);
}
}
}
Hoa Sen University 14
st = getSubtotal
lineItems[i] :
SalesLineItem
t = getTotal
loop
: Sale
Sequence diagram notation
Sequence diagram notation

Nesting frames
Hoa Sen University 15
calculate
: Bar
xx

[ color = red ]
opt
: Foo
loop(n)
Sequence diagram notation
Sequence diagram notation

Relate interaction diagrams

An interaction occurrence ( an
interaction use) is a reference to
an interaction within another
interaction.

Two related frames

A frame around an entire sequence diagram, labelled
with the tag sd and a name,

A frame tagged ref, called a reference, that refers to
another named sequence diagram;
Hoa Sen University 16
Sequence diagram notation
Sequence diagram notation
Hoa Sen University 17
interaction occurrence
note it covers a set of lifelines
note that the sd frame it relates to
has the same lifelines: B and C
doA

: A : B : C
doB
sd AuthenticateUser
ref
AuthenticateUser
authenticate(id)
doX
doM1
: B : C
authenticate(id)
doM2
ref
DoFoo
sd DoFoo
doX
: B : C
doY
doZ
Sequence diagram notation
Sequence diagram notation

Messages to classes to invoke static
methods
public class Foo{
public void doX(){
Locale[] locales = Calendar.getAvailableLocales();

}
}
Hoa Sen University 18

1: locs = getAvailableLocales
: Foo
«metaclass»
Calendar
doX
message to class, or a
static method call
Sequence diagram notation
Sequence diagram notation
Polymorphic messages and cases
Hoa Sen University 19
:Register
authorize
doX
:Payment {abstract}
polymorphic message
object in role of abstract
superclass
:DebitPayment
doA
authorize
:Foo
stop at this point – don’t show any
further details for this message
doB
:CreditPayment
doX
authorize
:Bar
Payment {abstract}

authorize() {abstract}

CreditPayment
authorize()

DebitPayment
authorize()

Payment is an abstract
superclass, with concrete
subclasses that implement the
polymorphic authorize operation
separate diagrams for each polymorphic concrete case
Sequence diagram notation
Sequence diagram notation

Asynchronous and synchronous calls

Call does not wait for a response

Asynchronous calls are used in multi-thread
application

Show as a stick arrow message

It is common for modeler to use stick arrow to
represent synchronous message

Do not assume the shape of the arrow is correct
Hoa Sen University 20

Sequence diagram notation
Sequence diagram notation
Hoa Sen University 21
Collaboration diagram notation
Collaboration diagram notation

Links

A connection path between two
objects

Messages flow along link

Messages

Message between objects is
represented with a message
expression and small arrow
indicating the direction of the
message

A sequence number is added to
show the sequential order of
messages in the current thread
of control

Many messages may flow
along a same single link
Hoa Sen University 22
1: makePayment(cashTendered)

2: foo
2.1: bar
: Register :Sale
link line
1: msg2
2: msg3
3: msg4
3.1: msg5
: Register :Sale
all messages flow on the same link
msg1
Collaboration diagram notation (cont)
Collaboration diagram notation (cont)

Message to “self”
or “this”

Creation of
instances

Use a message named
create for this purpose

Create message
may have
parameters

Indicates a constructor
call with parameters in
Java

Hoa Sen University 23
: Register
msg1
1: clear
Collaboration diagram notation (cont)
Collaboration diagram notation (cont)

Message number sequencing

OK to use flat numbering style or

Nested decimal numbers

Clarify whether a message is called within another
message
Hoa Sen University 24
: A
msg1
: B
1: msg2
: C
1.1: msg3
not numbered
legal numbering
Collaboration diagram notation (cont)
Collaboration diagram notation (cont)
Hoa Sen University 25
: A
msg1
: B

1: msg2
: C
1.1: msg3
2.1: msg5
2: msg4
: D
2.2: msg6
first second
fourth
sixth
fifth
third

×