Archive for July, 2009

Implementing Web Apps as Finite State Machines

Saturday, July 11th, 2009

The revelation that a web application meshes well with the design pattern of a Finite-State Machine (wikipedia link) isn’t really new. It’s discussed in a few other places on the web, usually in a scholarly capacity – the google search returns a lot of articles and research papers. Still, this struck me as something that really should be taught to every fledgeling web developer.

Wikipedia defines an FSM as “a model of behavior composed of a finite number of states, transitions between those states, and actions.

In other words, it’s a map of how your application works, with consideration for how you get from point A to B to C. The pattern can be used to design simple to very complicated web applications. Listy.us is currently being built around a fairly simple FSM pattern where every state ultimately  returns to an idle state when finished. Entering the state sets everything up: dynamic elements, events, etc. Exiting tears down the event namespace (see jQuery – Namespaced events) and does any data saving.

You can read about FSMs as applied to game development at this site (it’s more readable than the wiki entry). In fact, if the area of artificial intelligence and/or game development interests you, I’d recommend picking up the book by that site’s author.

Simple example:
start -> state:idle (enter, run)
-> (click field) -> transition:editMode (exit idle, enter editMode, run editMode)
-> (click out/keypress) -> transition:idle (exit editMode, enter idle, run idle)

Maybe I’ll clean up the FSM design and post it sometime.

Post to Twitter Tweet This Post Post to Digg Digg This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post