Ooad sequence diagram lecture

26
By Nadeem Mahmood Department of Computer Science University Of Karachi

description

object-oriented programming

Transcript of Ooad sequence diagram lecture

Page 1: Ooad sequence diagram lecture

By Nadeem Mahmood

Department of Computer ScienceUniversity Of Karachi

Page 2: Ooad sequence diagram lecture

Interaction Diagrams

Interaction diagrams describe exemplary how groups of objects collaborate in some behavior.

An interaction diagram typically captures the behavior of a single use case.

Interaction diagrams do not capture the complete behavior, only typical scenarios.

Page 3: Ooad sequence diagram lecture

Types of Interaction DiagramsThere are two types of interaction diagrams:

Sequence diagrams emphasize the order or concurrency of the interactions.

Collaboration diagrams emphasize the interacting objects.

Page 4: Ooad sequence diagram lecture
Page 5: Ooad sequence diagram lecture

Behavior of the “order” use case:A customer orders several products.The (sub-)orders (“order lines”) for each product are prepared separately.For each product check the stock.• If the product is in stock, remove requested amount from stock.• If the product stock falls below a predefined level, reorder it.

Page 6: Ooad sequence diagram lecture

ObjectObject naming:

syntax: [instanceName][:className]Name classes consistently with your class

diagram (same classes).Include instance names when objects are

referred to in messages or when several objects of the same type exist in the diagram.

The Life-Line represents the object’s life during the interaction

myBirthdy:Date

Page 7: Ooad sequence diagram lecture

MessagesAn interaction between two objects is

performed as a message sent from one object to another (simple operation call, Signaling, RPC)

If object obj1 sends a message to another object obj2 some link must exist between those two objects (dependency, same objects)

Page 8: Ooad sequence diagram lecture

Messages (Cont.)A message is represented by an arrow

between the life lines of two objects.Self calls are also allowedThe time required by the receiver object to process

the message is denoted by an activation-box.

A message is labeled at minimum with the message name.Arguments and control information (conditions,

iteration) may be included.

Page 9: Ooad sequence diagram lecture

Return ValuesOptionally indicated using a dashed arrow

with a label indicating the return value.Don’t model a return value when it is obvious

what is being returned, e.g. getTotal()Model a return value only when you need to

refer to it elsewhere, e.g. as a parameter passed in another message.

Prefer modeling return values as part of a method invocation, e.g. ok = isValid()

Page 10: Ooad sequence diagram lecture

Synchronous MessagesNested flow of control, typically implemented

as an operation call.The routine that handles the message is

completed before the caller resumes execution.

:A :B

doYouUnderstand()

Caller Blocked

return (optional)yes

Page 11: Ooad sequence diagram lecture

Object CreationAn object may create another object via a <<create>> message.

:A :B

<<create>>

Constructor

:A

<<create>> :B

Preferred

Page 12: Ooad sequence diagram lecture

Object DestructionAn object may destroy another object via a <<destroy>> message.An object may destroy itself.Avoid modeling object destruction unless memory

management is critical.

:A :B

<<destroy>>

Page 13: Ooad sequence diagram lecture
Page 14: Ooad sequence diagram lecture
Page 15: Ooad sequence diagram lecture
Page 16: Ooad sequence diagram lecture
Page 17: Ooad sequence diagram lecture

Why not just code it?Sequence diagrams can be somewhat close to the

code level. So why not just code up that algorithm rather than drawing it as a sequence diagram?

17

a good sequence diagram is still a bit above the level of the real code (not EVERY line of code is drawn on diagram)

sequence diagrams are language-agnostic (can be implemented in many different languages

non-coders can do sequence diagrams easier to do sequence diagrams as a team can see many objects/classes at a time on same page (visual bandwidth)

Page 18: Ooad sequence diagram lecture

Lifetime of objectscreation: arrow with

'new' written above itnotice that an object

created after the start of the scenario appears lower than the others

deletion: an X at bottom of object's lifelineJava doesn't explicitly

delete objects; they fall out of scope and are garbage-collected

18

Page 19: Ooad sequence diagram lecture

Indicating selection and loopsframe: box around part of a sequence

diagram to indicate selection or loopif -> (opt) [condition]if/else -> (alt) [condition], separated by horiz.

dashed lineloop -> (loop) [condition or items to loop over]

19

[balance <> 0]opt [balance < 100.00]

[balance >= 100.00]

alt

[balance < 0]loop

Page 20: Ooad sequence diagram lecture
Page 21: Ooad sequence diagram lecture
Page 22: Ooad sequence diagram lecture
Page 23: Ooad sequence diagram lecture
Page 24: Ooad sequence diagram lecture

Reference: Object-Oriented Analysis and DesignJ.W. Schmidt, F. Matthes, TU Hamburg-Harburg

Page 25: Ooad sequence diagram lecture

Reference: Object-Oriented Analysis and DesignJ.W. Schmidt, F. Matthes, TU Hamburg-Harburg

Page 26: Ooad sequence diagram lecture

Sequence diag. from use case

26