UML -Part 3

Post on 05-Jan-2016

25 views 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 of UML -Part 3

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

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

Sequence Diagram

4

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

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

Components

7

lifeline

execution

Components

8

Return value

Method call

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

Asynchronous Message Example

10

Synchronous messageAsynchronous messageReturn message

There are problems here… what are they?

Components: alt/else

11

Components: option

12

Components: loop

13

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

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

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

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

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

Example 1:Book in a Library System

19

New

Available

Reserved

Borrowed

start

activate return

borrow

reserverelease

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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