Back

OO diagrams - the state diagram

state1Some objects can behave differently, depending upon what situation or 'state' they are in. Because of this, it is useful to be able to show graphically these different states and how they behave. There are a number of different ways of drawing state diagrams, each of which use different symbols. Although they all show the same information, we should have a look at some different examples of producing these types of diagrams.

The purpose of the state diagram is to show:

   * an abstraction (a model) of the different states that an object can be in
   * the transitions between the states
   * the events that trigger those transitions.

A 'state' is the name we give to the situation an object might find itself in. To get in any particular state, an activity will have happened. When in any particular state, it either carries out some activity or other, or waits for something to happen, which might trigger a change in state. The idea of a 'state' can be confusing at first. For example, many student 'objects' go to school by bus.

   * When a student is in the state where they are actually getting on the bus: the activity to be allowed to get on the bus is to show the bus pass.
   * When a student is in the state where they are actually on the bus: the activities the student might do include finding a seat, and pressing the 'Stop' button when they near to where they want to get off. It might also include the student carrying out some sub-activities e.g. doing their homework or checking their phone for text messages.
   * When a student is in the state where they are actually getting off the bus: the student must remember to pick up their bag and coat.

Perhaps a relatively straight forward example of a state diagram is what happens to a book in a library. One state is that it is on the shelf. Another state is that it has been lent out. When a book is in the borrowed state, the event that can occur is to return it. When the book has been returned, the event that can occur is to lend it out. To get into the state of being borrowed, the event 'Borrow' must have happened. To be on the shelf, the event that must have happened was the book being 'Returned'.

This information is summarised in the diagram. You can see the states are represented by circles. The information inside the circle includes the name of the state, a reference number for the state and the event that occured to get the object into that state. The transition events that can occur when in a partiuclar state are shown by arrows leaving a state, and they include a description of the transition condition. 

state2In our next exam, we are going to produce a state diagram for a door. A door can be open, or closed or locked. The initial, starting state that the door is in is indicated by the black filled-in circle. In this example, the starting condition is the door being open.

A door cannot be open and locked at the same time, so you can see that the lock / unlock transitions can only be achieved from the closed and locked states. Neither can you close a door if there is something in the doorway. For that reason, you can see that the transition 'Close' has a condition attatched to it. This indicates that the transition is not allowed unless the condition is TRUE i.e. the doorway is not blocked.

If there were a finish state then this would be shown by a black filled-in circle surrounded by a circle. In this particular example, there is no defined end state so we don't have this.

Back