object state modeling for event- driven systems yonglei tao 1
TRANSCRIPT
1
Object State Modeling for Event-Driven Systems
Yonglei Tao
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.
Microwave Oven Control
Events and Guard Conditions
Composite States
Idle
Active[valid subscriber]
PlayingDialTone
Dialing Connecting
digitdigit
complete
Talking
connected
Hierarchical Statecharts
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
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
9
Use Case – Process Sale
10
State Machine Diagram
WatingForSale EnteringItems
enterItem
WaitingForPayment
makeNewSale
makeCashPayment
endSale
AuthorizingPayment makeCheckPayment
makeCreditPayment
authorized
Process Sale
Object State Modeling - Validate PIN
12
Object State Modeling Steps
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
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
15
A Cruise Control
16
Cruise Control Domain Model
17
Constructing a State Transition Table Constructing the state transition table is
optional It is a systematic approach to state behavior
modeling
18
Cruise Control State Transition Table
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
20
Converting to State Diagram
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()
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
23
Update Design Class Diagram Add methods labeling the transitions to the
subject class
CruiseControl
onOffButtonPressed()leverDown()leverUp()brakeApplied()leverDownAndHold()leverUpAndHold()leverPulled()leverReleased()setDesiredSpeed()
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
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
26
Traditional Approach
ch = getchar();switch (state) {
case ‘s’: if ( isalnum(ch) ) { state = ‘w’; wc++;
} else ......
case ‘w’: if ( ispuct(ch) ) { ......
}......
}
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
28
State Objects and Current State
:StateS :StateW :StateP
State currentState
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);
}
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();
}}
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();
}}
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();
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