State Modeling

State modeling is another very old software design technique.  It aims to model the way behaviour changes as a result of external input.

Now, the word ‘State’ is used in many ways when talking about software, and perhaps needs some definition first.  One way to think of  ‘state’ is as the perceived state of the system as judged from outside, perhaps called aggregate state.  Since facile analogies help here is one.  When the car is in the state ‘engine off’ then no amount of pressing the accelerator will get the car to move.  It does not matter if the handbrake is on, or the wheels are pointing straight ahead.  The handbrake setting and the wheel direction may be part of a complete description of the state of the car, but they matter little to us when deciding what to do next.  The ‘aggregate state’ is ‘engine off’ and there is only one trigger that will get us to the state of ‘engine running’.

Did you see that? I snuck in a new term – ‘trigger’.  A trigger is anything that causes (or could cause) the system to change state.  State modeling is about describing how the system responds to triggers.  Different combinations of trigger and state will have different results.  When a document editor is in the ‘editing’ state it will respond to key presses (‘triggers’) by changing the document,  but when it is in read-only mode it will ignore the same triggers (key presses).  Of course there is a special trigger that moves the editor from ‘read-only’ state to ‘editing’ state, and another that moves it back.  So you can see that triggers can change the state of the system, and the state determines its response to the triggers.

I have written a simple tool to illustrate this:  State Modeler.  It is written in java and requires Java 8 or higher.

The thing that I find most fascinating about state modeling is that, within a limited domain, a state model can be such a complete description that the behaviour can be automatically converted to code.  State Modeler does this, just to prove my contention, and will even generate a small testing class to demonstrate.

I will describe how to use the State Modeler in another post.  Please feel free to try it out before then if you wish.



Leave a Reply

Your email address will not be published. Required fields are marked *