You are on page 1of 37

IS223D: OBJECT-ORIENTED DESIGN

Information Systems
Department Lecture 6: State Machine Diagram
Objectives
2

 To explain what is dynamic modeling.


 To explain what is State Machine Diagram.

25-Feb-16 Information Systems Department


Dynamic Modeling
3

 All systems have static structure and dynamic


behavior.
 Class diagrams are best used to document and
express the static structure of a system—the classes,
objects, and their relationships.
 Class diagrams does not explain how things
cooperate to manage their tasks and provide the
functionality of the system.

25-Feb-16 Information Systems Department


Dynamic Modeling (cont.)
4

 How objects communicate and the effects of such


communication are referred to as the dynamics of a
system; that is:
 how the objects collaborate through communication
 how the objects within the system change state during
the system’s lifetime.

25-Feb-16 Information Systems Department


Dynamic Modeling (cont.)
5

 The dynamic diagrams described in this chapter


are:
 State machine diagrams
 Activity diagrams

 Interaction diagrams

25-Feb-16 Information Systems Department


State Machine
6

 Capture the life cycles of objects, subsystems, and


systems.
 They tell the states an object can have and how
events affect those states over time.
 A behavioral state machine should be attached to
all classes that have clearly identifiable states and
complex behavior; the state machine specifies the
behavior and how it differs depending on the
current state.

25-Feb-16 Information Systems Department


States and Transitions
7

 All objects have a state.


 The state is a result of previous activities
performed by the object and is typically
determined by the values of its attributes and links
to other objects.
 A class can have a specific attribute that specifies
the state, or the state can be determined by the
values of the “normal” attributes in the object.

25-Feb-16 Information Systems Department


States and Transitions (cont.)
8

 Examples of object states are:


 The invoice (object) is paid (state).
 The car (object) is standing still (state).

 The engine (object) is running (state).

 Jim (object) is playing the role of a salesman (state).

 Kate (object) is married (state).

25-Feb-16 Information Systems Department


States and Transitions (cont.)
9

 An object transitions (changes) from one state to


another state when something happens, which is
called an event; for example, someone pays an
invoice, starts driving the car, or gets married.

25-Feb-16 Information Systems Department


States and Transitions (cont.)
10

 State machine diagrams can have a starting point


and several endpoints.
 A starting point (initial state) is shown as a solid
filled circle, and an endpoint (final state) is shown
as a circle surrounding a smaller solid circle.

25-Feb-16 Information Systems Department


States and Transitions (cont.)
11

 A state is shown as a rectangle with rounded


corners. The name of the state is shown with text
inside the rounded rectangle or as a name tab
attached to the rounded rectangle.

25-Feb-16 Information Systems Department


States and Transitions (cont.)
12

 Between the states are state transitions, shown as a


line with an arrow from one state to another.
 The state transitions may be labeled with the event
causing the state transition.

25-Feb-16 Information Systems Department


States and Transitions (cont.)
13

25-Feb-16 Information Systems Department


States and Transitions (cont.)
14

 A state may contain two kinds of compartments:


 Name compartment :
 The name of the state, for example, idle, paid, and
moving.
 Action compartment (optional).

25-Feb-16 Information Systems Department


Action compartment
15

 Lists behavior in response to events.


 The formal syntax for the action compartment is:
event-name argument-list ‘/’ action-expression
 You can define your own event, such as selecting a
Help button, as well as the activity to respond to
that event.
 Three standard events names are reserved in UML:
entry, exit, and do.

25-Feb-16 Information Systems Department


Action compartment (cont.)
16

 The event-name can be any event, including the


standard events, entry, exit, and do.
 The action-expression tells which action should be
performed (for example, operation invocations,
incrementing attribute values, and so on).
 It is also possible to specify arguments to the events
(entry, exit, and do events do not have any
arguments).

25-Feb-16 Information Systems Department


Action compartment (cont.)
17

 The entry event can be used to specify actions on


the entry of a state; for example, assigning an
attribute or sending a message.
 The exit event can be used to specify actions on exit
from a state.
 The do event can be used to specify an action
performed while in the state; for example, sending
a message, waiting, or calculating.
 These standard events cannot be used for other
purposes.
25-Feb-16 Information Systems Department
Action compartment (cont.)
18

25-Feb-16 Information Systems Department


States and Transitions (cont.)
19

 A state transition normally has an event attached to


it, but it is not necessary.
 If an event is attached to a state transition, the state
transition is performed when the event occurs.
 If a state transition does not have an event
specified, the attached state changes when all
internal actions in a state are performed.

25-Feb-16 Information Systems Department


States and Transitions (cont.)
20

25-Feb-16 Information Systems Department


Event-Signature
21

 Event-signature, which consists of an event-name


and parameters, specifying the event that triggers
a transition
 The syntax of parameters:
Parameter-name ‘:’ type-expression, Parameter-name ‘:’ type-expression
...
 The parameter-name is the name of the parameter
and the type-expression is the type of the
parameter, for example, integer, Boolean, and
string.
 The type-expression may not shown.
25-Feb-16 Information Systems Department
Event-Signature (cont.)
22

25-Feb-16 Information Systems Department


Guard-Condition
23

 Is a Boolean expression placed on a state transition.


 If the guard-condition is combined with an event-
signature, the event must occur and the guard-
condition must be true for the transition to fire.
 If only a guard-condition is attached to a state
transition, the transition fires when the condition
becomes true.
 Examples:

25-Feb-16 Information Systems Department


Guard-Condition (cont.)
24

25-Feb-16 Information Systems Department


Action-Expression
25

 Action-expression is a procedural expression


executed when the transition fires.
 It is possible to have more than one action-
expression on a state transition, but they must be
delimited with the backward slash (/) character.
 It is possible to have a state-transition that contains
only an action-expression.
 Examples:

25-Feb-16 Information Systems Department


Action-Expression (cont.)
26

25-Feb-16 Information Systems Department


Simple and Composite States
27

 Simple States :simplest of all states, they have no


substates.
 Composite States - have one or more regions for
substates.
 A region is simply a container for substates.
 A composite state with one region is called non-
orthogonal.
 A composite state with two or more regions is called
orthogonal.
25-Feb-16 Information Systems Department
Non-orthogonal composite state
28

 Only one of the substates at a time.


 ATM example:

25-Feb-16 Information Systems Department


Orthogonal composite state
29

 Concurrent regions are independent and can


complete at different times.
 Each region is separated from the others by a
dashed line.

25-Feb-16 Information Systems Department


Orthogonal composite state (cont.)
30

 Maintenance state from ATM example:

25-Feb-16 Information Systems Department


Example 1
31

 The watch displays the current time and it has a


single mode button and a single advance button.
Pressing the mode button once allows setting the
hours. Each press of the advance button increments
the hour by one. Pressing the mode button the
second time allows setting the minutes. Each press of
the advance button increments the minute by one.
Pressing the mode button a third time displays the
current time. Pressing the mode button allows the
user to set the hour again.
 Draw a state machine diagram.
25-Feb-16 Information Systems Department
Example 1 (cont.)
32

25-Feb-16 Information Systems Department


Example 2
33

 To take IS340, a student must go through the


following process. When the instructor posts
assignment 1, each student must form a team, do
the assignment and hand it in. When assignment 2 is
posted, each student works on it with her team; in
parallel, when the midterm is made available, each
student writes it. After both assignment 2 and the
midterm are done with, each student waits for
assignment 3, and when it is available, does it with
her team.
25-Feb-16 Information Systems Department
Example 2 (cont.)
34

 Draw a state diagram that models this process.


Make sure to model activities (e.g., doing an
assignment, writing a test) as states, not transitions.

25-Feb-16 Information Systems Department


Example 2 (cont.)
35

25-Feb-16 Information Systems Department


Class Activity
36

 Draw a state machine diagram describing the


operation of a phone.
 The user dialing the number, if he/she enters a
wrong digit the calling will be invalid. If the dialing
digits valid, the phone will connecting. The tone may
be ringing tone or busy tone.

25-Feb-16 Information Systems Department


Reference
37

 “UML 2 Toolkit”, Hans-Erik Eriksson , Magnus Penker,


Brian Lyons, and David Fado, Chapter 5

25-Feb-16 Information Systems Department

You might also like