0% found this document useful (0 votes)
20 views15 pages

Lecture 10 Statediagrams

Uploaded by

ffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

Lecture 10 Statediagrams

Uploaded by

ffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

ITSE 2F12

Software Engineering
Lecture 10
UML State Diagrams

Reading:
UML Distilled, Ch. 10, M. Fowler

slides adopted from Marty Stepp http://www.cs.washington.edu/403/


UML state diagrams
• state diagram: Depicts data and behavior of a single
object throughout its lifetime.
– set of states (including a start state and end states)
– transitions between states due to an event
– entire diagram is drawn from that object's perspective

• Here is a State chart for a Phone Line object state

initial State

event

transition
2
State diagram example 2
– removing candle reveals lock, which you can open using
key
– Otherwise get devoured by a killer rabbit

3
When to develop a state
chart?
• Model objects that have change state in interesting
ways:
– Devices (microwave oven, Ipod)
– Complex user interfaces (e.g., menus)
– Transactions (databases, banks, etc.)
– Stateful sessions (server-side objects)
– Controllers for other objects
– large, complex objects with a long lifespan

• Not useful to do state diagrams for every class in the


system!

4
States
• state: conceptual description of the data in the object
– represented by object's field values
– E.g If Lock class has an isWaiting field
and it is set to true – we can say we are
in Waiting state.

• entire diagram is drawn from the


central object's perspective
– only include states / concepts that
this object can see and influence
– don't include every possible value
for the fields; only ones that are
conceptually different
5
Transitions
• transition: movement from one state to another

• signature [guard] / activity


– signature: event that triggers (potential) state
change
– guard: boolean condition that must be true
– activity: any behavior executed during transition
(optional)

6
Internal activities
• internal activity: events that the
central object takes on itself
– sometimes drawn as self-transitions
(events that stay in same state)

• entry/exit activities
– reasons to start/stop being in that state

7
User account management
State diagram example

8
Exercise
• Draw a state diagram for an order object.

9
Super/substates
• If there are states share common transitions and
internal activities
– You can make them substates
• Caution: Don't over-use this feature.
– easy to confuse separate states for sub-states within one
state

10
ATM software states at a bank
State diagram example

11
State diagram example
Java thread states

12
Implementing states
• What are some ways to write
code to match a state diagram?
– state tables (pseudo-code)
– nested if/else
– switch statements
– state enums
– State design pattern

13
State pattern
• state pattern: An object whose sole purpose is to represent
the current "state" or configuration of another larger object.
–A behavioral pattern (more on Design Patterns course).
–Often implemented with an enum type for the states.
–Each object represents one specific state for the larger object.
–The larger object sets its state in response to various mutations.
–Allows various observers and interested parties to quickly and
accurately know what is going on with the larger object's status.

• Analogous to the notion of finite state machines.


–Set of states (nodes)
–Set of edges (mutations that cause state changes)

14
State enum example
/** Represents states for a poker game. */
public enum GameState {
NOT_STARTED, IN_PROGRESS, WAITING_FOR_BETS,
DEALING, GAME_OVER;
}

/** Poker game model class. */


public class PokerGame {
private GameState state;

public GameState getState() { return state; }

public void ante(int amount) {


...
state = WAITING_FOR_BETS; // change state
setChanged();
notifyObservers(state);
}
}
15

You might also like