33
Object State Modeling for Event-Driven Systems Yonglei Tao 1

Object State Modeling for Event- Driven Systems Yonglei Tao 1

Embed Size (px)

Citation preview

Page 1: Object State Modeling for Event- Driven Systems Yonglei Tao 1

1

Object State Modeling for Event-Driven Systems

Yonglei Tao

Page 2: Object State Modeling for Event- Driven Systems Yonglei Tao 1

2

Key Takeaway Points Object state modeling

concerned with the identification, modeling, design, and specification of state-dependent, reactive behavior of objects.

The state pattern reduces the complexity of state behavior

design and implementation, and makes it easy to change.

Page 3: Object State Modeling for Event- Driven Systems Yonglei Tao 1

Microwave Oven Control

Page 4: Object State Modeling for Event- Driven Systems Yonglei Tao 1

Events and Guard Conditions

Page 5: Object State Modeling for Event- Driven Systems Yonglei Tao 1

Composite States

Idle

Active[valid subscriber]

PlayingDialTone

Dialing Connecting

digitdigit

complete

Talking

connected

Page 6: Object State Modeling for Event- Driven Systems Yonglei Tao 1

Hierarchical Statecharts

Page 7: Object State Modeling for Event- Driven Systems Yonglei Tao 1

Guidelines A state name must reflect an identifiable

situation Idle, Waiting for PIN

It must be possible to exit from every state Cyclic behavior

Do not confuse events and actions

Page 8: Object State Modeling for Event- Driven Systems Yonglei Tao 1

8

User Interface Design Interaction design

Structure of the dialog between the user and the computer

Visual design Screen element selection Screen layout Visual characteristics of screen elements

Page 9: Object State Modeling for Event- Driven Systems Yonglei Tao 1

9

Use Case – Process Sale

Page 10: Object State Modeling for Event- Driven Systems Yonglei Tao 1

10

State Machine Diagram

WatingForSale EnteringItems

enterItem

WaitingForPayment

makeNewSale

makeCashPayment

endSale

AuthorizingPayment makeCheckPayment

makeCreditPayment

authorized

Process Sale

Page 11: Object State Modeling for Event- Driven Systems Yonglei Tao 1

Object State Modeling - Validate PIN

Page 12: Object State Modeling for Event- Driven Systems Yonglei Tao 1

12

Object State Modeling Steps

Page 13: Object State Modeling for Event- Driven Systems Yonglei Tao 1

13

Collecting and Classifying State Behavior Information

What to look for Example Classification Rule

Something interest happened

An online application submitted.

Event E1

Mode of operation A cruise control operates in activated/ deactivated modes.

State S2

Conditions that govern the processing of an event

Turn on AC if roomtemperature is high

Guardcondition

G1

An act associates with an event

Push the lever down to set the cruising speed.

Response R1

Page 14: Object State Modeling for Event- Driven Systems Yonglei Tao 1

14

Constructing a Domain Model The domain model shows relationships

between the state dependent software and its context

State Machin

e

Source & Destination 1 event 1a

event 1b

event 2aevent 2b

event 3aevent 3bevent 3c

resp. 1xresp. 1y resp. 2x

resp. 2y

resp. 3x

Source & Destination 2

Source & Destination 3

Page 15: Object State Modeling for Event- Driven Systems Yonglei Tao 1

15

A Cruise Control

Page 16: Object State Modeling for Event- Driven Systems Yonglei Tao 1

16

Cruise Control Domain Model

Page 17: Object State Modeling for Event- Driven Systems Yonglei Tao 1

17

Constructing a State Transition Table Constructing the state transition table is

optional It is a systematic approach to state behavior

modeling

Page 18: Object State Modeling for Event- Driven Systems Yonglei Tao 1

18

Cruise Control State Transition Table

Page 19: Object State Modeling for Event- Driven Systems Yonglei Tao 1

19

Use of State Transition Tables A systematic approach to ensure

completeness every state-event combination is analyzed

Easy to detect dead state unreachable state neglected event impossible transitions nondeterministic transitions redundant transitions inconsistent transitions

Allow to generate state diagrams automatically

Page 20: Object State Modeling for Event- Driven Systems Yonglei Tao 1

20

Converting to State Diagram

Page 21: Object State Modeling for Event- Driven Systems Yonglei Tao 1

21

Cruising

Cancelled

Converting Texts to Function Calls

leverPulled(),brakeApplied()

onOffButtonPresse

d()

Cruise Deactivat

ed

Increasing

Speed

Decreasing

Speed

onOffButton Pressed()

Cruise Activated

leverDown()/ setDesiredSpee

d(),

Cruising

leverDown() / setDesiredSpee

d()

leverReleased()/

setDesiredSpeed()

leverReleased() / setDesiredSpeed()

leverUpAnd Hold()

leverUpAnd Hold()

leverDown AndHold()

leverUp()

leverDown

AndHold()

Page 22: Object State Modeling for Event- Driven Systems Yonglei Tao 1

22

Implementation of a Control Class An event in the state diagram implies a call to

a method of the class An instance variable determines the current

state of an instance of the class A method evaluates the state variable and the

guard condition, executes the appropriate response actions, and updates the state variable

Page 23: Object State Modeling for Event- Driven Systems Yonglei Tao 1

23

Update Design Class Diagram Add methods labeling the transitions to the

subject class

CruiseControl

onOffButtonPressed()leverDown()leverUp()brakeApplied()leverDownAndHold()leverUpAndHold()leverPulled()leverReleased()setDesiredSpeed()

Page 24: Object State Modeling for Event- Driven Systems Yonglei Tao 1

24

The State Pattern Develop a program that reads a text file

(such as proj1.c) and counts the number of words the number of symbol sequences in a text

State-dependent behavior how to process each input character depends on

its current state

Page 25: Object State Modeling for Event- Driven Systems Yonglei Tao 1

25

State Diagram

letter, d ig itw c++

w hite space

letter, d ig it

w hitespace

w hite space

punctuationpc++

Punctuationpc++

letter, d ig itw c++

W

P

S

punctuation

symbol

symbol

symbol

Page 26: Object State Modeling for Event- Driven Systems Yonglei Tao 1

26

Traditional Approach

ch = getchar();switch (state) {

case ‘s’: if ( isalnum(ch) ) { state = ‘w’; wc++;

} else ......

case ‘w’: if ( ispuct(ch) ) { ......

}......

}

Page 27: Object State Modeling for Event- Driven Systems Yonglei Tao 1

27

Using the State Pattern

Document

+Parse() : void-currentState : State

+Next(ch : char) : State

StateS

+Next(ch : char) : State

StateW

+Next(ch : char) : State

StateP

+Next(ch : char) : State

<<Interface>>State

Page 28: Object State Modeling for Event- Driven Systems Yonglei Tao 1

28

State Objects and Current State

:StateS :StateW :StateP

State currentState

Page 29: Object State Modeling for Event- Driven Systems Yonglei Tao 1

29

Parsing a Text Line

char[] line = new char[Maxsize];char ch;State currentState = new StateS();

read a line of text into linefor (int i=0; i<line.length(); i++){

ch = line[i];

currentState = currentState.Next (ch);

}

Page 30: Object State Modeling for Event- Driven Systems Yonglei Tao 1

30

Method Next in State S

public State Next (char ch) {

if ( Character.isLetterOrDigit (ch) ) {wc++;return new StateW();

} else if (Character.isSpaceChar (ch) )

return new StateS(); else {

pc++;return new StateP();

}}

Page 31: Object State Modeling for Event- Driven Systems Yonglei Tao 1

31

Method Next in State W

Public State Next (char ch) {

if (Character.isLetterOrDigit (ch) ) return new StateW(); else if (Character.isSpaceChar (ch) )

return new StateS(); else {

pc++;return new StateP();

}}

Page 32: Object State Modeling for Event- Driven Systems Yonglei Tao 1

32

Method Next in State P

public State Next (char ch) {

if (Character.isLetterOrDigit (ch) ) {wc++;return new StateW();

} else if (Character.isSpaceChar (ch) )

return new StateS(); else

return new StateP();

Page 33: Object State Modeling for Event- Driven Systems Yonglei Tao 1

33

Benefits Isolate behavior that the context object

shows in each state Easy to add/remove a state and to change

the behavior of a state