[Q] Enum style Object?

Ned Konz ned at bike-nomad.com
Wed Feb 19 18:03:58 UTC 2003


On Tuesday 18 February 2003 08:45 pm, Eric Merritt wrote:
> This question probably has an obvious solution but I
> can't see it at the moment.
>
> For the most part my Java experience has proved to be
> useful in aqueak (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 tehn have
> a series of static final bytes that represent each
> state, in C/C++ I would use an enum. I can't, however,
> for the life of me figure out how to translate this to
> squeak. I guess maybe the states  would be class vars
> initialized  in the class initializer but this seems a
> bit strange to me. Would one of you point me in the
> right direction?

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: ]

etc.

-- 
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE



More information about the Squeak-dev mailing list