uml -part 3

44
UML -Part 3

Upload: ferris

Post on 05-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

UML -Part 3. Dynamic Diagram Types. Interaction Diagrams - Set of objects or roles and the messages that can be passed among them. Sequence Diagrams - emphasize time ordering Communication Diagrams - emphasize structural ordering State Diagrams - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UML -Part  3

UML -Part 3

Page 2: UML -Part  3

Dynamic Diagram Types

• Interaction Diagrams - Set of objects or roles and the messages that can be passed among them.

–Sequence Diagrams - emphasize time ordering–Communication Diagrams - emphasize structural ordering

• State Diagrams–State machine consisting of states, transitions, events and

activities of an object

• Activity & Swimlane Diagrams–Emphasize and show flow of control among objects

2

Page 3: UML -Part  3

Sequence Diagrams

•Describe the flow of messages, events, actions between objects

•Show concurrent processes and activations

•Show time sequences that are not easily depicted in other diagrams

•Typically used during analysis and design to document and understand the logical flow of your system

3

Page 4: UML -Part  3

Sequence Diagram

4

Page 5: UML -Part  3

Sequence Diagram

5

Tim

e Increasing -->

All lines should be horizontal to indicate instantaneous actions. Additionally if ActivityA happens before ActivityB, ActivityA must be above activity A

Page 6: UML -Part  3

Components

6

Objects: aStudent is a specific instance of the Student class

Specific Instance of an

Object

Generic (unnamed)objectsGeneric (unnamed)

objects of class type Seminar and Course

Page 7: UML -Part  3

Components

7

lifeline

execution

Page 8: UML -Part  3

Components

8

Return value

Method call

Page 9: UML -Part  3

Components

9

c : Client

: Transaction

o : ODBCProxy

create()

setActions(a, b, c)setValues(a, 3, 4)

setValues(b, c, 7)

(committed)

destroy()

Synchronous messageAsynchronous message

create()destroy()

Return message

Page 10: UML -Part  3

Asynchronous Message Example

10

Synchronous messageAsynchronous messageReturn message

There are problems here… what are they?

Page 11: UML -Part  3

Components: alt/else

11

Page 12: UML -Part  3

Components: option

12

Page 13: UML -Part  3

Components: loop

13

Page 14: UML -Part  3

Rules of thumb

• Rarely use options,loops,alt/else – These constructs complicate a diagram and make them

hard to read/interpret. – Frequently it is better to create multiple simple diagrams

• Create sequence diagrams for use cases when it helps clarify and visualize a complex flow

• Remember: the goal of UML is communication and understanding

14

Page 15: UML -Part  3

Summary

• Sequence diagrams model object interactions with an emphasis on time ordering

• Method call lines– Must be horizontal! – Vertical height matters!

“Lower equals Later”– Label the lines

• Lifeline – dotted vertical line• Execution bar – bar around lifeline when code is

running• Arrows

– Synchronous call (you’re waiting for a return value) – triangle arrow-head– Asynchronous call (not waiting for a return) – open arrow-head– Return call – dashed line

15

Page 16: UML -Part  3

In class exercise

• Draw a sequence diagram for:– Getting on a flight. Start at home, check in at the

counter, go through security, and end up at the gate. (If you have time during the exercise, get yourself to your seat.)

• You may get searched in security

16

Page 17: UML -Part  3

In class exercise

• Draw a sequence diagram for:– Getting money from an ATM machine

• Treat each part of the ATM as a class– Money dispenser– Screen– Keypad– Bank computer– Etc…

17

Page 18: UML -Part  3

State Diagrams

• Depicts object behavior across use cases• State: collection of values of attributes• State-behavior relationship

– state is updated by some behavior carried out

• Notation– States: rounded rectangles– Arrows: state transitions– Labels on Arrows: event/action/use case

18

Page 19: UML -Part  3

Example 1:Book in a Library System

19

New

Available

Reserved

Borrowed

start

activate return

borrow

reserverelease

Page 20: UML -Part  3

What Are Statechart Diagrams?

• A statechart diagram shows a state machine, which specifies the sequences of states that an object can be in, the events and conditions that cause the object to reach those states, and the actions that take place when those states are reached.

20

Sample State 1

Sample State 2

Page 21: UML -Part  3

Drawing States• A state is represented as a rounded rectangle

on a statechart diagram. • As a comparison, note the subtle difference

between a state versus an activity.

21

Sample State 1

SampleActivity

Page 22: UML -Part  3

State and Attributes

• States may be distinguished by the values of certain attributes.

The maximum number of students per course is 10.

numStudents < 10 numStudents > = 10

22

English 101 : Course

ClosedOpen

Course

- numStudents

Page 23: UML -Part  3

States and Links• State may also be distinguished by the existence

of certain links.• Instances of class Professor can have two states

– Teaching when a link to a course exists.– On sabbatical when no link exists.

23

Professor<<entity>>

Course<<entity>>

0..*11 0..*

Page 24: UML -Part  3

Special States• The initial state is the state entered when an object is

created.– An initial state is mandatory.– Only one initial state is permitted.– The initial state is represented as a solid circle.

• A final state indicates the end of life for an object.– A final state is optional.– More than one final state may exist.– A final state is indicated by a bull’s eye.

24

Sample State 1

Sample State 2

Page 25: UML -Part  3

What Are Events?• An event is the specification of a significant

occurrence that has a location in time and space.– An event is an occurrence of a stimulus that can

trigger a state transition.– Example

• Adding a student to a course• Creating a new course

25

State BState A Event

Page 26: UML -Part  3

What Are Transitions?• A transition is a change from an originating state to a

successor state as a result of some stimulus.– The successor state could possibly be the originating state.

• A transition may take place in response to an event.• Transitions can be labeled with events.

26

State BState A Event

Transition

Page 27: UML -Part  3

What Are Guard Conditions?• A Boolean expression that is evaluated when

the transition is triggered by the reception of the event trigger. If the expression evaluates to True, the transition is eligible to fire. If the expression evaluates to False, the transition does not fire.

27

State A Event[ thisAttribute >= 3 ] State B

Guard Condition

Page 28: UML -Part  3

What Are Actions?

• Executable atomic computations that result in a change in state of the model or the return of a value– Associated with a transition– Take an insignificant amount of time to complete – Non-interruptible

28

State A Event[ thisAttribute >= 3 ] / Action State B

entry/ Action

Actions

Page 29: UML -Part  3

What Are Activities?• Ongoing, non-atomic executions within a state

machine– An operation that takes time to complete– Starts when the state is entered– Can run to completion or can be interrupted by an

outgoing transition

29

State B

entry/ Actiondo/ on Undefined/

State A Event[ thisAttribute >= 3 ] / Action

Activities

Page 30: UML -Part  3

Send Events

• One event may trigger the sending of another event.

• An activity can also send an event to another object.

30

State B

entry/ Actiondo/ on Undefined/ ^Send Event

Event[ thisAttribute >= 3 ] / Action ^Send Event

State A

Page 31: UML -Part  3

What Are Substates?

• Statechart diagrams can become very complex.• Substates (nested states) can be used to simplify

complex diagrams.• A superstate is a state that encloses nested states

called substates.• Any number of levels of nesting are permitted.• Nested states can lead to substantial reduction of

graphical complexity, allowing us to model larger and more complex problems.

31

Page 32: UML -Part  3

Statechart Diagram Without Substates

32

add student / numStudents = numStudents + 1

Unassigned

Assigned

Full

Cancelleddo: Send cancellation notices

Committed

do: Generate class roster

closeRegistration [ has Professor assigned ]

close

/ numStudents = 0

addProfessor

closeRegistration

remove student / numStudents = numStudents - 1

cancel

removeProfessor

[ numStudents = 10 ]

close[ numStudents < 3 ]

closeRegistration[ numStudents >= 3 ]

close[ numStudents >= 3 ]

add student /numStudents = numStudents + 1

cancel

remove student / numStudents = numStudents - 1

close

[ numStudents = 10 ] cancel

Page 33: UML -Part  3

Statechart Diagram with Substates

33

superstate

substate

add student /numStudents = numStudents + 1

Open

Unassigned

Assigned

H

add a professor

Closed

Cancelleddo: Send cancellation notices

Full

Committeddo: Generate class roster

closeRegistration

close

remove a professorclose[ numStudents < 3 ]

[ numStudents = 10 ]

closeRegistration[ numStudents >= 3 ]

close[ numStudents >= 3 ]

closeRegistration [ has Professor assigned ]

close

/ numStudents = 0

remove student / numStudents = numStudents - 1

cancelcancel

Page 34: UML -Part  3

When to DrawState Diagrams

• State diagrams are good at describing the behavior of an object across several use cases

• Use state diagrams only for those classes that exhibit interesting behavior, often the main classes of the system

34

Page 35: UML -Part  3

State Diagrams Help Complete System Design

• State diagrams often reveal use cases overlooked in earlier analyses of the system

• State diagrams provide hints on which attributes are necessary for a given class

35

Page 36: UML -Part  3

Activity Diagram

• Represents sequence of activities• Depicts condition, iteration, and parallel

activities• When to use:

– Analyzing use cases• Alternate way of describing a use case• Depicting use case sequences/workflow

– Describing method behavior

36

Page 37: UML -Part  3

Activity Diagram Notation

• Oval - activity• Start and end buttons• Links between ovals to depict sequence• Diamond – conditional branch and merge• Bar – fork and join (concurrent activities)

37

Page 38: UML -Part  3

About Activity Diagrams

• Helps in understanding system behavior, but … it does not provide an object-oriented perspective!

• Suggestion: just use as a tool to clarify complex sequential/parallel relationships between behavioral components

38

Page 39: UML -Part  3

CRC Cards

• Not really part of the UML– But helps in construction of Class Diagrams and

Interaction Diagrams• CRC: Class-Responsibility-Collaboration• Role-playing:

– Team members represent classes– Each member/class holds an index card

39

Page 40: UML -Part  3

CRC Cards, continued

• Team goes through each use case and role-play object interaction

• Responsibilities (which end up being methods) are noted on the index card

• Use of an index card is deliberate– Responsibilities ought to be evenly distributed across the classes

that interact– Overloaded classes become immediately apparent

40

Page 41: UML -Part  3

CRC Cards and UML Diagrams

• Role-playing activity for each use case maps directly to interaction diagram construction

• Each card provide class details (methods) for the class diagram

41

Page 42: UML -Part  3

Activity Diagram

42

Supplements the use-case by providing a diagrammatic representation of procedural flow

Details:

-Start is a single circle

-End is a bulls-eye

-Decisions are diamonds (guards must be on both branches of the diamond!)

Questions:

Do we always have a start?

Do we always have an end?

Page 43: UML -Part  3

Swimlane Diagrams

43

Allows the modeler to represent the flow of activities described by the use-case and at the same time indicate which actor (if there are multiple actors involved in a specific use-case) or analysis class has responsibility for the action described by an activity rectangle

Page 44: UML -Part  3

Activity Diagram Example

To show concurrent activity, activity diagrams allow branches and joins.

You can also reference or include other activity diagrams

44

Fork/Branch

Join