
Java Secret: Using an enum to build a State machine - javacodegeeks
http://www.javacodegeeks.com/2011/07/java-secret-using-enum-to-build-state.html
======
breckinloggins
The page crashed in Chrome, fyi (EDIT: once. I think it was an ad that was
served).

Also, I'm hesitant to call anything regarding enums and State Machines a
"secret", no matter the language. What IS cool about Java enums, though, is
that they can encapsulate behavior. Building a state machine with C# or C
enums involves defining the enum and then writing a giant switch statement
(often in several places) or making a generic callback registration engine.

EDIT: Are there any other languages that do cool things with enums like this?
I was delighted the first time I learned you could do this in Java.

One way around this if you're in, say, C#, is to just forget enums and use a
static class...

    
    
      class State {
        static State Init = () -> // do stuff
        static State Running = () -> // do running stuff
        static State Close = () -> // do closing stuff
        
        static void NewState(State state)  {
          state()
        }
    

And you can reference the states just like enums:

    
    
      if (someState == State.Init) // then do something with it
    

(I know this isn't syntactically correct C#... I haven't used the language in
over a year now.)

~~~
icco
Page didn't crash in Chrome Beta on Linux.

And I agree, while good knowledge, I don't think this is a secret. I remember
a simpler example of this being taught in some of my intro CS classes...

As for doing this in other languages, I think Java's ability to encapsulate is
pretty unique... but I'm not sure. In theory you should be able to do
something similar in the "pure" OO languages (Ruby, Python, SmallTalk, etc).

~~~
gnaritas
FYI, Smalltalk is one word, not two crammed together.

------
smackfu
I wish it had gone more into the advantages to this, over the standard method
of switching on a state variable.

~~~
peter_lawrey
Using this approach could be more logical, esp as the number of methods per
state increases. As you have more complex events and you want to group as
State/Method rather than Method/State this approach is more appealying.

------
ZoFreX
Java enums are very underused and rather awesome. This article misses out
members and constructors, which is a pity.

~~~
peter_lawrey
In a state machine, all the state should be in the context rather than the
code blocks. The Java Tutorial on enums talks about fields and constructors.

------
jimdesu
You can build cons-lists or other initial-algebraic data-types with them very
nicely. I <3 them.

------
earl
The website is so distracting I couldn't even pay attention to the content.
Not only is there a bloody ad in the middle of the first paragraph forcing the
text to wrap around the ad, but at most 15% of the above the fold site is
actual content. Swaths of the page are devoted to really nothing at all
(enormous header and sidebar), ads, and useless whitespace. And are the
pennies the author gets for running ads like that really worth making the site
look like shit?

[https://img.skitch.com/20110712-pff65sbxas9t89w6ecckqjq9ic.j...](https://img.skitch.com/20110712-pff65sbxas9t89w6ecckqjq9ic.jpg)

edit: dear authors: if you're going to share content, _share content_. Not
header, sidebar, adds, rss, follow me on twitter, stumble, +1, bing, like,
toolbars, etc. There's a reason people fucking love readability.

~~~
pan69
Its a content farm, probably to count ad impressions or some other BS. This is
the original (from the bottom of the article):

[http://vanillajava.blogspot.com/2011/06/java-secret-using-
en...](http://vanillajava.blogspot.com/2011/06/java-secret-using-enum-as-
state-machine.html)

