Fast and simple state machine implementation
Last updated at 12:48 am UTC on 17 January 2006
Date: Wed, 19 Feb 2003
From: Ned Konz
Subject: Re: [Q] Enum style Object?
Eric Merritt wrote:
> For the most part my Java experience has proved to be useful in Squeak (Squeak is like the garden of eden compared to Java :) but I am having trouble getting my mind around a state machine. In Java I would just create a state instance var thats a byte and then have a series of static final bytes that represent each state, in C/C++ I would use an enum. How do I implement this in Squeak?
Ned Konz answered:
There are several ways to do it. If you want to do a really fast and simple state machine, make your state variable a symbol that names the method to handle events in the current state:
state := #initial:.
later, to dispatch an event:
self perform: state with: anEvent
and in #initial:
anEvent isCharacter ifTrue: [ ... do something ...
state := #someOtherState: ]
[20-FEB-03] If this explanation is too terse please ask more on the mailing list with a [DOCS] tag referring to this page. hjh