State Diagram - Example

Suppose you're designing a toaster. You would build a plenty of UML diagrams, but here only state diagrams will be of our interest.

- What are the steps of making a toast?

First of all we must turn on the toaster, put in the bread and wait for several minutes to bake it. The initial state diagram is shown below:

stateDiagram1.GIF (1886 bytes)
The initial state diagram for making a toast

But this is not the final state diagram. To prevent burning out the bread, heater of the toaster must produce heat in temperature interval (upper and lower temperature limits). For this purpose thermometer measures the temperature of heater, and when the upper limit of temperature is reached then heater must go into idle state. This state resists until heater's temperature decreases to lower limit, and then working state is again aimed. With this new state, extended state diagram will be:

stateDiagram2.GIF (1945 bytes)
The extended state diagram for making a toast

Transition between working and idle state is not presented in details. To do this substates must be added.

stateDiagram31.GIF (1958 bytes)
stateDiagram32.GIF (1923 bytes)
Substates in Working and Idle states

Substates in working and idle states are very similar. both had measure and compare states, but differentiates in process of temperature comparison. Working state must compare current temperature with upper temperature limit (if it is reached, working state goes into idle state), and idle state compares current temperature with lower temperature limit (idle state is replaced with working state when temperature falls under lower limit).

It's necessary to have state diagrams because they help analysts, designers and developers understand the behavior of the objects in a system. Developers, in particular, have to know how objects are supposed to behave because they have to implement these behaviors in software. It's not enough to implement an object: Developers have to make that object do something.