55
Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Object Oriented Development with UML

By Assoc. Prof. Rangsit Sirirangsi

State and Activity Diagrams

Page 2: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Difference between me and Sundar Pichai

Page 3: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

State Diagram

State ไดอาแกรมเกดขนมากอนวธการออกแบบเชงวตถ โดยมวตถประสงคในการแสดงการท างานของระบบ หรอแมแตน าไปใชในการก าหนดนยามในรปของการท างานของระบบ

state ไดอาแกรมแสดงใหเหนถงการเปลยนแปลงสถานะของออปเจคหนง ๆตามชวงเวลาในการตอบสนองตอการเปลยนแปลงคาของแอททรบวตหรอเหตการณทเกดขน

เนองจากสถานะของออปเจคจะแสดงใหเหนถงแนวโนมในการท างานทจะเกดขนตอไปในอนาคต

เพอลดความสบสนในการออกแบบ วธการเชงวตถจะอาศยการก าหนด state ไดอาแกรมไวส าหรบแตละคลาสแทน เพอใหการสรางไดอาแกรมสามารถท าไดงายขน

Page 4: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

What Is an Object?

ในการโปรแกรมเชงวตถ ออปเจคถอเปนตวแทนของสงใดสงหนง ซงอาจอยในรปของวตถทางกายภาพ (physical) หรอแนวคด (conceptual) หรอแมกระทงซอฟตแวร (software)

โดยปกตแลวแตละออปเจคจะประกอบไปดวยคณสมบต 3 ประการ ไดแกIdentity แสดงถงแตละออปเจคจะมเอกลกษณเฉพาะทตางกน แม State จะเหมอนกบออปเจคอน

State เปนคณสมบต ซงไดแก ขอมลทอยภายในออปเจคบางครงจะถกเรยกวาแอทรบวต (attributes)

Behavior เปนพฤตกรรมการท างาน (behavior) ของออปเจคซงสงผลตอคาสถานะของออปเจคเอง

Page 5: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

State Diagram

แมวา Sequence diagram จะชวยใหนกออกแบบมองเหนล าดบการท างานของหลาย ๆ ออปเจคทมความสมพนธกบเมธอดทถกเรยกใชในชวงเวลาหนง แตไมลกลงไปถงระดบกลไกการท างานภายในออปเจค

ตวอยางเชน ออปเจคของคลาสหองพกดงตอไปน ไมสามารถแสดงการท างานผาน Sequence diagram ได

Vacancy Occupied

Booking Reserved

Book CancelCancel

ReserveCheck in

Reserve

Check Out

Page 6: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

State (Transition) Diagrams

วตถประสงคเพอท าความเขาใจกบสถานะของออปเจคพฒนา state diagrams ในการสรางแบบจ าลองเชงพฤตกรรม

สงทตองพจารณา :วธการก าหนดสถานะความเปนไปไดของออปเจคออปเจคใดทม state อยางมนยส าคญ ไดแก ออปเจคทสามารถมองเหนสถานะไดอยางชดเจน

ไมมความจ าเปนทจะท าการสรางสเตทไดอาแกรมส าหรบทก ๆ ออปเจควธการท state diagrams ถกแปลงใหเปนแบบจ าลองอน ๆ

Page 7: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Switch On/Offแนวความคดเบองตนของ State Diagram เปนการก าหนดรายละเอยดของสถานะตาง ๆ ทเกดขนกบระบบ โดยรบคา events ทเกดขน แตละ event จะกอใหเกดการเปลยนแปลงจากสถานะหนงไปยงสถานะอน

เชน สวทชไฟประกอบไปดวย 2 สถานะ ไดแก: up และ down (หรอ "on" และ "off") ใน UML state ไดอาแกรมแตละสถานะจะถกน าเสนอโดยรปสเหลยมมมมน

สวทชไฟจะมความเปนไปไดของเหตการณทจะเกดขนเพยงหนงแบบ: เมอมการกดสวทช ซงอาจเรยกใชส าหรบสองเหตการณทแตกตางกน เปนตน

Page 8: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

What are the components?

State – สถานะปจจบนของออปเจคถกก าหนดโดยคาปจจบนของแอททรบวตEvent – เปนแมสเสจทสงจากออปเจคอนหรอเปนไปตามเงอนไขทก าหนดGuard – นพจนแบบบลนใชเปน triggers ของ event ทกอใหเกด transitionAction – การตอบสนองตอการเปลยนแปลงสถานะโดยออปเจคTransition – การเปลยนแปลงสถานะภายในออปเจค

Working Shutting

Down

terminate

Screen

Saving

keyStroke

Empty [is Timeout]/popUpScreenShot()

turn PC onBooting

Shutting

down

State

EventGuard

Action

Transition

Load OS

Page 9: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Definitions : State

ชวงชวตของออปเจคทเปนไปตามเงอนไขทก าหนด และกอใหเกด action หรอรอรบ eventสถานะของออปเจคสามารถน าไปใชส าหรบการน าเสนอการท างานทผานมาของออปเจค

สถานะปจจบนของออปเจคถกก าหนดโดยคาปจจบนของแอททรบวตทถกก าหนดไวในออปเจค

State Name

stateVar : type = value

entry/ entry activitydo/ activityexit/ exit activity

State

Transition

Other State Name

Page 10: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Transitions

การเปลยนแปลงจาก state หนง ๆ ของออปเจคไปยง State อนทเกดขนโดยevent

ใน UML เปนมมมองของความสมพนธระหวางสองสถานะทระบวาออปเจคในสถานะหนงก าลงจะเขาสสถานะถดไป transition ประกอบไปดวย :event signature –> ชอของ event รวมกบ parameters guard condition –> เงอนไขแบบ Boolean ซงก าหนดใหเปน true เมอมการระบ guard condition event จะตองเกดขนและเงอนไขจะตองเปนจรงเพอใหtransition เกดขน

action –> atomic expression ทถกประมวลผลเมอ transition เกดขน

Page 11: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Event-Transition Notation

State 2. . .

State 1do/activity 1

event [condition] /action

ชอของ event ทท าใหเกด transition การท างานของ action เมอเกด event

เงอนไขกอนท transition จะเกดขน

Event(arguments)

[condition]

/operation(arguments)

event name

guard

action

trigger}

Page 12: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

States Diagram: Symbolics

Initial stateเกดขนเมอออปเจคถกสรางขนตองก าหนดไวในไดอาแกรมเสมอ initial state สามารถมไดเพยงหนงเดยวภายในไดอาแกรม

Final state ใชส าหรบแสดงการสนสดการท างานของออปเจคถอเปนทางเลอกอาจมไดมากกวาหนงในชวงชวตของออปเจค

Final state

Initial state

Page 13: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Definitions cont.Eventเปนเหตการณทเกดขนและอาจกอใหเกดการเปลยนแปลงของ state โดยไมตองมก าหนดชวงเวลาไว และสามารถเกดขนกอนหรอหลง event ใด ๆ

เปนสงเกดขนแลวอาจกอใหเกด action บางอยางอาจอยในรปของแมสเสจทสงจากออปเจคอน ๆevent สามารถน าขอมลจากออปเจคหนงไปยงออปเจคอน ๆ ได เชน class : Course

event: enroll(studentId)

Page 14: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Guard condition

Guard-Condition เปนนพจนแบบ boolean ทใชส าหรบตรวจสอบ event ทกอใหเกด transition

เงอนไขแบบ Boolean ใชส าหรบการทดสอบสถานะภายในของออปเจคได เชน[birthdate < current_date] ถกประเมนผลในรป TRUE/ FALSE

Actionการตอบสนองตอการเปลยนแปลง state ของออปเจค สวนทเปน action-

expression จะถกประมวลผลและกอใหเกด Transition อยในรปของ atomic expression เสมอ

Page 15: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Guards

เงอนไขการปองกน (guard condition) ใชส าหรบการก าหนดสถานะทมโอกาสเกดขนไดมากกวาหนงสถานะ โดยปกตจะอางถงคาของแอททรบวตทจะเกดขนหลงจากเงอนไขทก าหนดไวเปนจรง

Selling

Unhappy

Happy

bid [(value >= 100) & (value < 200)] /sell

bid [value >= 200] /sell

bid [value < 100] /reject

Page 16: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Elevator : State Diagram 1

state diagram ส ำหรบลฟท (elevator)

On First

Floor

Moving Up Idle Moving Down

Moving to

First Floor

arrived

go up go down

arrived

time- outgo up

arrived

ลฟทจะเรมตนจากชนลางสดและสามารถเลอนขนหรอลงไดหากลฟทรออยทชนใดชนหนง เมอ time-out event เกดขนหลงจากชวงเวลาหนง ลฟทจะเลอนลงมารอทชนลางเสมอ

Page 17: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Activities within the State

เปนกจกรรมการท างานทเกยวกบ State ทใชเวลาชวงหนง ๆ จนกระทงเสรจสนActivity จะมผลท าใหเกด Transition ออกไป ไมวาจะท าจนเสรจสนหรอถกขดจงหวะโดย Event

รปแบบตาง ๆ ของ Activity ภายใน state :event nameentry / action : กจกรรมทตองท าเสมอเมอ

ออปเจคเขาส state นน ๆdo / Activity: กจกรรมอน ๆ ทตองท าใหเสรจ

ภายใน stateexit / action : กจกรรมทตองท าเสมอเมอ

ออปเจคออกจาก state นน ๆ

State Name

stateVar : type = value

entry/ entry actiondo/ activityexit/ exit action

Page 18: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Elevator : State Diagram 3การน าเสนอ state diagram เพอแสดง event/actions

Idle

On First

Floor

Moving Up

do: moving to

floor

Moving Down

do: moving

to floor

Moving to

First Floor

arrived

go up( floor ) go down( floor )

arrived

[ timer =time- out ]go up( floor )

arrived

entry: timer = 0

do: increase timer

Page 19: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

State Diagram : Order

Dispatching

do: initiate delivery

Waiting

Delivered

Item received[ some

items not in stock ]

Item received[ all

items available ]

Delivered

Cancelled

Checkingdo: check

item

[ not all items checked ]

/ get next item

/ get first item

[ All items checked &&

some items not in stock ]

[ All items checked &&

all items available ]

cancelled

cancelled

cancelled

Page 20: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Composite StateChart Diagrams

Nested statechart diagram เปนไดอาแกรมทมมากกวา 1 state regionออปเจคจะมเพยง 1 state ใน 1 ของ state diagrams ใชส าหรบ State ทเปนเงอนไขแบบ or-condition บนทก ๆ statechart

diagramsใช abstract/generalize states ในรปของ state เดยว

Concurrent statechart diagramออปเจคจะมเพยง 1 state ใน 1 ของ statechart diagrams ใชส าหรบ state ทเปนเงอนไขแบบ and-condition บนทก ๆ statechart

diagramsใชส าหรบแสดงการท างานแบบ multi-threading

Page 21: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Complex StateChart Diagram

ในกรณท State diagrams มขนาดใหญและซบซอนมาก อาจจ าเปนตองใช Nested states เพอยอมใหผใชสามารถโมเดลปญหาทมขนาดใหญและซบซอนสงได

โดย superstate จะเปน state ทหอหม nested states ทเรยกวา substates ไว

Superstate

State-A State-BState-A State-BEvent A

Event B Event C

Page 22: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Superstate / Substates

Dispatching

do: initiate delivery

Waiting

Delivered

Item received[ some

items not in stock ]

Item received[ all items available ]

Delivered

Cancelled

Checking

do: checkitem

[ not all items checked ]

/ get next item

/ get first item

[ All items checked && some

items not in stock ]

[ All items checked && all items available ]

Active

cancelled

Page 23: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Transmission

turn key to start

Concurrency Among StateChart Diagrams

Neutral Reverseselect R

select Nselect Fselect N

SecondFirst Third

Forward

upshift upshift

downshiftdownshift

stop

Ignition

Off

releasekey

turn key off

Starting On

guardreferencesTransmissionstatechartdiagram

[Transmissionin Neutral]

Page 24: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Rules of thumb

state diagram ไมจ าเปนจะตองถกสรางขนกบทก ๆ คลาส ทใชกนบอย ๆ มกจะเปน State diagram ทไมซบซอนState diagrams บอยครงจะใชกบงานประเภท UI concurrent ในการท างานจะเกดขนเฉพาะภายในออปเจคเดยว เมอใดทควรใช StateChart Diagramsเหมาะสมอยางยงทจะใชส าหรบแสดงรายละเอยดของการท างานของออปเจคทเกดขนในหลาย ๆ use cases

ใชเฉพาะกบคลาสทเปนคลาสส าคญทม dynamic behavior sequence diagrams สามารถใชในการศกษาเพอคนหา dynamic ออปเจค

Page 25: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Class definition

package Traveling;

public class Car {

private Location currentLocation;

private java.sql.Date builtDate;

}

public class Location {

private java.lang.String city;

}

Car

currentLocation : LocationbuiltDate : Date

start( )stop( )goto( )

Location

city

getCity( )setCity( )

Page 26: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Adding methods

public class Car {

private Location currentLocation;

private java.util.Calendar builtDate;

public void start ( ) {

return;

}

public void stop ( ) {

return;

}

public void gotoLocation (Location newLoc ) {

currentLocation = newLoc;

}

}

Missing: accessing

methods

Car

currentLocation : LocationbuiltDate : Date

start ()stop ()goto (newloc : Location = Calgary)

Page 27: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

State transitions

Car

currentLocation : LocationbuiltDate : Datestate : StringgasAvailable : Boolean

start ()stop ()goto (newloc : Location = Calgary)

public class Car {

private Location currentLocation;

private java.util.Calendar builtDate;private String state;

private boolean gasAvailable;

public void start ( ) {

if (gasAvailable) {state = "started“;}

return;

}

public void stop ( ) {

state = "stopped";

return;

}

public void gotoLocation (Location newLoc ) {

currentLocation = newLoc;

}

}

stopped started

start[ gas available ]

stop

Page 28: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Process For Deriving State Diagrams

ก าหนด states และรายละเอยดส าหรบออปเจคก าหนด events ทท าใหเกด state transitionsก าหนดสถานะ from และ to states ทเกยวของกบแตละ state transition เพม activities และ actions ทเกยวของกบ state transition

Page 29: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

The example we will use

Class CourseOfferingก าหนดคาเรมตน studentsNo = 0จ านวนสงสด studentNo = 10จ านวนต าสด = 3เกดการ cancelled เมอ จ านวนนกศกษาต ากวา min

มการ closed เมอนกศกษา = maxEventsinitializeadd studentremove student

Page 30: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Step 1: Identify and Define the States, find links

สถานะของออปเจคทเปน dynamic attributes

numStudents < 10

Open

จ านวนนกศกษาสงสดตอ course offering จะเทากบ 10numStudents > = 10

(guard condition)

Closed

state เปนหนงในเงอนไขความเปนไปไดทอาจเกดขนภายในออปเจค ซงอาจระบไดในรปของ attribute values

Course is Open Closed

Page 31: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Identify start State and transitionsก าหนดจดเรมตนของสถานะแตละ state ก าหนดวา events ใดทกอใหเกด transitions ไปยง states ใดรวมถงก าหนด guard conditions ทจ าเปน

Transitions แสดงรายละเอยดสงทเกดขนในการตอบสนองผลลพธของ event state transition เปนการเปลยนแปลงจาก state หนงไปยง state อน

Initialize Open

AddStudent( Student ) /

set student = 0

addStudent( Student )

[ student < 10 ]

Closed

[ student = 10 ]

Cancelled

Cancel Course

Page 32: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Find events and send events

event หนง ๆ อาจเปน trigger ไดโดยการสงสญญาณไปยง event อน ๆจ านวนนกศกษาครบ 10 คน มการสง Event ทเรยกวา compileReport เพอสราง Report เปนตน

จาก Open ไปจนถง Close

compileReportInitialize Open

AddStudent( Student ) /

set student = 0

addStudent( Student )

[ student < 10 ]

Closed

[ student = 10 ]

Cancelled

Cancel Course

CourseReport.

Create report]

Page 33: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Stop State

ก าหนด stop state ซงเปนสถานะสดทายของออปเจคจ านวนของ stop state อาจมมากกวา 1 หรออาจไมมเลยได

Initialize Open

AddStudent( Student ) /

set student = 0

addStudent( Student )

[ student < 10 ]

Closed

[ student = 10 ]

Cancelled

Cancel Course

Page 34: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Standard Events for a StateStandard events ทอาจเกยวของกบแตละ stateEntry - ระบ actions เมอเขาส stateExit - ระบ actions เมอออกจาก stateDo - ระบ activities ทเกดขนใน state

Initialize

Open

entry/ Register Student

AddStudent( Student )

/ set student = 0

addStudent( Student )

[ student < 10 ]

Closed

do/ Finalize Course

exit/ Set Status to Close

[ student = 10 ] /

CourseReport.Create report

Cancelled

Cancel Course

Page 35: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Detailed CourseOffering class

Initialize

do: Initialize course

objectdo: Assign lecturer

to course

Open

entry: Register a

student

Closed

do: Report

course is full

Canceled

do: Send cancellation

notices

addStudent/

numStudents = 0

cancelCourse

RegistrationComplete

do: Generate class

roster

cancelCourse

[ numStudents = 10 ]

cancelCourse

registration closed[

numStudents > = 3 ]

registration closed[

numStudents < 3 ]

Unassigned

addStudent

Page 36: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

States and Objects

คาของแอททรบวต (stateVar) จะตองเปนตวก าหนด stateเมอออปเจคอยในสถานะทแตกตางกนจะตองมการตอบสนองตอ

event ทแตกตางกนตามไปดวยstate transitions สามารถเกดขนการระบ states จากพฤตกรรมทชดเจนหรอการตอบสนองของออปเจค

activity สามารถเปน state หรอกอใหเกด state transition

Page 37: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

How Do State Diagrams Map to the Rest of the Model?

Events สามารถ map เปน operationsMethods จะถก updated พรอมกบขอมลเฉพาะของ stateStates ถกน าเสนอผาน attributesใชส าหรบเปน input ในขนตอนการก าหนดคา Attributes

Open

addStudent( Student )

Closed

[ student = 10 ]

Courseoffering

student : Integer

addStudent(Student )

Page 38: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

The caller picks up the phone. A dial tones is heard on the phone line. The caller dials 7 digits. A dialling sound is heard on the phone line. After the seventh digit, the phone rings. The callee answers the phone.

Phone Call example:

Page 39: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Activity Diagram

Activity Diagram: ใชแสดง workflow ระหวางหลาย ๆ ออปเจคของ use case หรอ flow of control ส าหรบการท างาน ถอเปน Statechart diagram ชนดหนง แตจะแตกตางกนตรงทแสดงการ flow จาก activity ไปยง activity มลกษณะคลายกบ flowchart แตมความสามารถในการแสดงการ

ท างานแบบ concurrency แสดงการ flow ของ activities ไมใชของออปเจค

ใชส าหรบแสดงการกระท า (Action) ทเกดขน เมอมการประมวลผลการท างาน เชน การเรยกใชเมธอด เปนตน

แสดงวธการตาง ๆ ทเกยวของกบการกระท าทอาจเกดขนในขณะท างาน และวธการทมผลตออปเจคทเกยวของ

Page 40: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Analysis: UML Activity Diagram

Reviewmodel

Consolidatemodel

Defineentity

Defineboundary

Definecontrol

Defineinteractions

Defineassociations

Defineattributes

Definenontrivial

behavior

Defineparticipating

Define

use cases

objects

objects objectsobjects

Page 41: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Activity diagram หรอบางครงอาจถกเรยกวา “activity graph” ประกอบไปดวยล าดบของกจกรรมทเกดขนActivity : เปนล าดบของการกระท าทเกดขนในชวงเวลาทแนนอน และสามารถขดจงหวะได โดยแตละ activity มการสราง actions

Actions เปนการท างานหนง ๆ ทมผลตอการเปลยนแปลงสถานะหรอการคนคาทไมสามารถขดจงหวะได (ตามมมมองของผใช)

Transitions ภายใน activity diagram ไมมการก าหนด labelsใชส าหรบระบความสมบรณของ action หรอแสดงล าดบของ actionstransitions เหลานไมไดขนอยกบเหตการณจากภายนอก

Introduction

Page 42: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Activity Diagram Notations

1. Initial node – ใชสญลกษณเชนเดยวกบ State ไดอาแกรม

2. Actions – ใชสญลกษณ rounded rectangles เพอน าเสนอกจกรรมทประกอบไปดวย action ทเกดขน

3. Flow – ใชสญลกษณลกศรเพอระบความกาวหนาผาน actions สญลกษณ flows สวนใหญไมจ าเปนตองใชค าอธบายประกอบ

4. Decision – ใชสญลกษณ diamond แทน โดยมทง flow เขาและออก flows ทออกจากสญลกษณจะถกระบเปน conditions

Charge

Account

Page 43: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Activity Diagram Notations (cont.)

6. Fork – สญลกษณ black bar ทประกอบดวย 1 flow เขาและ 2 flow หรอมากวาออกActions ทเกดขนจะเปนการท างานภายใต Fork แบบขนาน

6. Join –สญลกษณ black bar ทประกอบไปดวย 2 flows หรอมากกวาเขาและ 1 flow ออก ทก ๆ actions ทเขาสการท างานแบบ join จะตองเสรจสมบรณกอนการด าเนนการในกระบวนการถดไป

7. Activity final – ใชสญลกษณแบบเดยวกบ state ไดอาแกรม

Page 44: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Getting Start

Select site

Commission architect

Develop plan

Bid plan

Do site work Do trade work

Finish construction

Initial state

: Certificate Of Occupancy

[completed]

Action

State

Sequential branch

Fork

Join

Final State

Activity with

Sub Machine

[else]

[not accepted]

Page 45: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

การท างานท Activity สนบสนน

Sequence จะเปนการท างานทเกดขนตามล าดบ อาจถอไดวาเปนแนวความคดพนฐานหลกของการเขยนโปรแกรม

Selection จะเปนการท างานในลกษณะทเปนทางเลอก โปรแกรมภาษาสวนใหญจะสนบสนนการท างานโดยใชชดค าสงประเภท if then else หรอ case

Iteration เปนการท างานในลกษณะวนซ าตามเงอนไขทก าหนด โปรแกรมภาษาสวนใหญจะสนบสนนการท างานโดยใชชดค าสงประเภท loop ตาง ๆ

Parallel จะเปนสถานะการณทการกระท าตาง ๆ สามารถเกดขนไดในทก ๆล าดบของความเปนไปได ผลลพธการท างานจะเหมอนเดมไมวาล าดบการท างานแบบใดจะถกเลอกใชกอน

Page 46: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Fork & Join

Fork และ Join เปนการระบการท างานแบบขนาน (parallel )

Fork – เกดขนในกรณทม 1 incoming transition และหลาย ๆ outgoing transitions ทมการท างานเปนแบบขนาน

Join – เกดขนเมอมหลาย ๆ incoming transitions และม outgoing transitionเพยงหนงเดยว

Synch mouth Stream Audio

Fork

Decompress

Clean up

Join

Page 47: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Swimlanes

activity diagram จะแสดงใหผใชทราบวาเกดอะไรขนภายในระบบ แตจะไมไดแสดงวาใครทท าใหเกดสงเหลานน

Activity ไดอาแกรมอาจน าเสนอโดยใชมมมองทเรยกวา "swimlanes" เพอเพมความชดเจนของการท างานทเกดขน โดยการจดกลมของ Activity ไวดวยกน

swimlanes แสดงใหเหนวา who/what ท าหนาทรบผดชอบตอ activity นนบอยครงทจะแบงการท างานของคลาสทเกยวของกนจ าเปนตองจดรปแบบของ activity diagram ในแนวตง

แตละ activity จะถกก าหนดใหอยใน 1 swimlane (zone)transitions อาจแสดงขาม lanes ได

Page 48: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Request product

Customer

Receive order

Sale

Process order

Warehouse

Pull material

Ship order

Bill customer

Pay billClose order

Swimlanes Activity

Page 49: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Request product

Customer

Object Flow

Receive order

Sale

Process order

Warehouse

Pull material

Ship order

Bill customer

Pay bill

Close order

o : Order

[in progress]

o : Order

[filled]

b : Bill

[paid]

b : Bill

[unpaid] Object

Status

Swimlanes Activity with Object

Page 50: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Activity Diagram : Code Details

while (true) {

amount = 0.0;

while (amount < price) {

wait for a coin;

add coin value to amount;

}

show all available soft drink;

while (selection is not done) {

wait for selection from user;

if selection is “eject coins” {

dispense coins;

set selection to “done”;

}

else if selection is a valid soft drink {

dispense change & soft drink concurrently;

set selection to “done”

}

}

}

Page 51: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

When to Use Activity Diagrams

Strength – สนบสนนการท างานแบบขนานWeakness – ไมสามารถสรางความชดเจนในสวนของ links ระหวางออปเจคขอด: วเคราะห use case ใหรายละเอยดกบอลกอลทมสทซบซอน ใชกบโปรแกรมประยกตประเภท multi-threaded

ขอดอย: การแสดงการท างานของออปเจคตาง ๆ มลกษณะเปน Action-oriented มากกวาทจะเปน object oriented

Page 52: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

When to use these diagrams ?

ความเหมาะสมในการท างานของทง 3 ไดอาแกรมState diagram : เหมาะสมในการแสดงรายละเอยดการท างานของออปเจคทท างานอยในหลาย ๆ ยสเคส

interaction diagram: เหมาะสมในการแสดงรายละเอยดการท างานในหลาย ๆ ออปเจคภายในหนงยสเคส

activity diagram : เหมาะสมในการแสดงรายละเอยดการท างานตามล าดบของการกระท าส าหรบหลาย ๆ ออปเจคและยสเคส

Page 53: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Business Process Model : Hotel Reservation

กระบวนการส ารองหองพกเรมตนจากความตองการของลกคาเปนหลก โดยการตรวจสอบหองวาง ในกรณทมหองวาง ลกคาด าเนนการส ารองหองพก รายละเอยดการส ารองหองพกจะถกยนยนโดยลกคา จากนนมสถานการณหนงในสเกดขน

a. ลกคาเขาพกตามก าหนดการณส ารองหองพกb. ลกคาอาจมการยกเลกc. ลกคาอาจมการแกไขรายละเอยดตาง ๆd. ลกคาอาจไมปรากฏตวตามก าหนดการณส ารองหองพก แตอยางไรก

ตามจะตองเสยคาปรบ

Page 54: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Identify Use cases from Business Process

Amend

Reservation

Check

Make

Confirm

Wait for

Amend

Take up

Cancel

Process Notify billing

enquiry/

[suitable

room]

[else]

amendment

request/

customer arrives/

cancel request/

no-show/

System

Reservation

Maker

Guest

Take Up

Reservation

Cancel

Reservation

Process

No-Show

Make

Reservation

no-show system

reservation

availability

reservation reservation

reservation

event

reservation

Page 55: Object Oriented Development with UML State and Activity ... · Object Oriented Development with UML By Assoc. Prof. Rangsit Sirirangsi State and Activity Diagrams

Example: Included Use Case

Reservation system

Guest

BillingSystem

Make a

reservation

Take up a

reservation

Cancel a

reservation

Amend

reservation

Process no

shows

Identify

reservation

<<include>>

<<include>>