You are on page 1of 29

State Diagrams

Introduction
• A State chart diagram describes a state machine. Now to clarify it state
machine can be defined as a machine which defines different states of an
object and these states are controlled by external or internal events.
• As State chart diagram defines states it is used to model lifetime of an object.
• State chart diagram is one of the five UML diagrams used to model dynamic
nature of a system.
 State chart diagrams are used to design interactive systems that respond to
either internal or external event.
 State chart diagram visualizes the flow of execution from one state to another
state of an object.
 The primary purpose of a state chart diagram is to model interactive systems
and define each and every state of an object. 
Introduction
• They define different states of an object during its lifetime. And
these states are changed by events.
• So State chart diagrams are useful to model reactive systems.
Reactive systems can be defined as a system that responds to
external or internal events.
• State chart diagram describes the flow of control from one state to
another state.
• States are defined as a condition in which an object exists and it
changes when some event is triggered.
Introduction
• So the most important purpose of State chart diagram is to model
life time of an object from creation to termination.
• State chart diagrams are also used for forward and reverse
engineering of a system. But the main purpose is to model reactive
system.
 Following are the main purposes of using State chart diagrams:
 To model dynamic aspect of a system.
 To model life time of a reactive system.
 To describe different states of an object during its life time.
 Define a state machine to model states of an object.
Introduction
 State chart diagrams are used to describe various states of an
entity within the application system.
 Two types of state machine diagrams
 Behavioral state machine
– It captures the behavior of an entity present in the system.
– It is used to represent the specific implementation of an element.
– The behavior of a system can be modelled using behavioral state
machine diagrams.
 Protocol state machine
– These diagrams are used to capture the behavior of a protocol.
– It represents how the state of protocol changes concerning the
event. It also represents corresponding changes in the system.
– They do not represent the specific implementation of an element.
Common Properties and its
notations
 Initial State
 Final State
 State-box
 State machine
 Decision box
Types of State
 Unified Modeling Language defines three types of states:
 Simple state
– They do not have any substrate.
 Composite state
– These types of states can have one or more than one substrate.
– A composite state with two or more substates is called an
orthogonal state.
 Submachine state
– These states are semantically equal to the composite states.
– Unlike the composite state, we can reuse the submachine states.
 Following rules must be considered while drawing a state
chart diagram:
 The name of a state transaction must be unique.
 The name of a state must be easily understandable and
describe the behavior of a state.
 If there are multiple objects, then only essential objects
should be implemented.
 Proper names for each transition and an event must be
given.
State Diagrams
 It describes the behavior of a system.

 It describes all of the possible states that a particular object


can get into and how the object’s state changes as a result
of events that reach the object.

 It is drawn for a single class to show the lifetime behavior


of a single object.
How to draw Statechart
Diagram?
• State chart diagrams are very important for describing the states.
States can be identified as the condition of objects when a
particular event occurs.
• Before drawing a State chart diagram we must have clarified the
following points:
 Identify important objects to be analyzed.
 Identify the states.
 Identify the events.
State Diagram
Example
• The following is an example of a State chart diagram where the state
of Order object is analyzed.
• The first state is an idle state from where the process starts. The next states are
arrived for events like send request, confirm request, and dispatch order. These
events are responsible for state changes of order object.
• During the life cycle of an object (here order object) it goes through the following
states and there may be some abnormal exists also. This abnormal exit may occur
due to some problem in the system. When the entire life cycle is complete it is
considered as the complete transaction as mentioned below.
State Diagrams
 The syntax for a transition label has three parts, all of
which are optional.

 Event[Guard] /Action

 State has an activity associated with it, indicated by a label


with syntax do/activity.
State Diagrams
 Actions are associated with transitions and are considered
to be processes that occur quickly and are not interruptible.

 Activities are associated with states and can take longer. It


may be interrupted by some event.

 A guard is a logical condition that will return only “true”


or “false”. A guarded transition occurs only if the guard
resolves to true.
State Diagrams
 Only one transition can be taken out of a given state, so we
intend the guards to be mutually exclusive for any event.

 A transition “cancelled” can be introduced when we want to


cancel an order at any point before it is delivered.

 A separate transition from each of the checking, waiting and


dispatching states can be drawn.

 An alternative is to create super state of all three states and then


draw a single transition from that. The sub states inherit any
transition on the super state.
State diagram without super states
State diagram with super states
Concurrent State Diagrams
 In addition to states of an order that are based on the
availability of the items, there are also states that are based
on payment authorization.
Payment Authorization
Concurrent State Diagrams
 The concurrent sections of the state diagram are places in
which at any point, the given order in two different states,
one from each diagram.

 When the order leaves the concurrent states, it is in only a


single state.
Concurrent State Diagram
When to use state diagram?
 State chart diagrams are used
 To model objects of a system.
 To model and implement interactive systems.
 To display events that trigger changes within the states.
 State chart diagrams are used to capture the changes in various
entities of the system from start to end.
 Use state diagrams for those classes that exhibit interesting
behavior, where building the state diagram helps you understand
what is going on.

 Don’t try to draw them for every class in the system.


Where to use State chart
Diagrams?
• State chart diagrams are used to model dynamic aspect of a system like other
four diagrams discussed in this tutorial. But it has some distinguishing
characteristics for modeling dynamic nature.
• State chart diagram defines the states of a component and these state changes are
dynamic in nature. So its specific purpose is to define state changes triggered by
events. Events are internal or external factors influencing the system.
• State chart diagrams are used to model states and also events operating on the
system. When implementing a system it is very important to clarify different
states of an object during its life time and state chart diagrams are used for this
purpose. When these states and events are identified they are used to model it
and these models are used during implementation of the system.
State machine Flow Chart

It represents various states of a The Flowchart illustrates the


system. program execution flow.

The state machine has a WAIT The Flowchart does not deal with
concept, i.e., wait for an action or waiting for a concept.
an event.
State machines are used for a live Flowchart visualizes branching
running system. sequences of a system.

The state machine is a modeling A flowchart is a sequence flow or


diagram. a DFD diagram.

The state machine can explore Flowchart deal with paths and
various states of a system. control flow.
Main usages of State chart
Diagram
• To model object states of a system.
• To model reactive system. Reactive system consists of reactive
objects.
• To identify events responsible for state changes.
• Forward and reverse engineering.
Summary
 State chart diagrams are also called as state machine diagrams.
 These diagrams are used to model the event-based system.
 A state of an entity is controlled with the help of an event.
 There is a total of three types of state machine diagrams: 1)
Behavioral 2) State machine 3) Protocol state machine
 State chart diagram is used to capture the dynamic aspect of a
system.
 A state is a specific moment in the lifespan of an object.

You might also like