• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

State pattern

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -

I was discussing the State Pattern (using Kathy's book, "Head First Design Patterns") a while back with some fellow Java enthusiasts. The CS Engineers in the group complained about the illustrated pattern examples. I could barely follow the conversation as some of the terms they were using were not in the book. When I got home I started Googling the terms like crazy (terms like Epsilon transition, etc.)

After thinking about this over the weekend, I began to wonder if a State Pattern and a State Machine are really different things. A state machine seems far, far more complicated.

Are they the same thing?
[ September 24, 2007: Message edited by: Charles McGuire ]
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not the same thing.

The State Pattern may contain a State Machine but its state transition logic could be decentralized.
Regardless of how the transition logic is implemented inside the class (which implements the State Pattern), its states and transitions can be described through a State Machine Diagram (Agile Modeling: Guidelines).

A State Machine Diagram can also be represented as a State Transition Table.

A State Transition Table can be used to generate a class that implements it in a centralized fashion as a Finite State Machine (FSM - UML Tutorial: Finite State Machines (PDF)).
See: Object Mentor: SMC - Finite State Machine Compiler (Java) (ZIP)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neat links. This FSM Link shows both the nested switch case solution and the State Pattern solution. This other FSM Link does not.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And neither link shows my personal favourite idiom:



I find this makes for much more concise and readable code in many common cases than either solution in the linked article.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank, to me you code absolutely fails to communicate what it is doing at all...
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja, that surprised me.

Maybe I have worked with too many state machines (or too few !)

I tend to think of a state machine as a finite set of possible {state, transition, new state} tuples. Each tuple is optionally associated with some behavior which occurs "on" the transition.

From this mental model, a natural representation is to list out each of the valid tuples, together with any associated code. The representation I showed in my code example seems (to me, at least) a fairly concise and efficient coding of such a representation.

Do you work from a different mental model of a state machine?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That brings some memories from way back. I built "combined" logic like that in a parser with "state + token." I numbered the bubbles in a RTN of the syntax for states and made a spreadsheet of {state,token,method,newstate} and loaded it at runtime. The generic engine was something like

Many of the methods shared state in the dombuilder, so separate command objects would have been more challenging.

Obviously, I ain't no compiler builder. This was for a class and it seemed important to write all the code, so no cool parser libraries.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank,

now I understand the code. The concept of adding state and transition seems to be quite "clever" (that is, non-intuitive) to me. I was also wondering about the meaning of the magic numbers. No I understand that there actually is none...

I see how, once you have groked the concept, this can be seen as an elegant solution. I'd probably still prefer one where the tuples are more explicit, or where the clever processing of the tuples is better decoupled from their definition. (I really like the way Robert Martin's FSM-source code looks like.)
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for interest, you can see a real implementation using my approach in the Stringtree XML parser
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja wrote: The concept of adding state and transition seems to be quite "clever" (that is, non-intuitive) to me.

I agree.

The only real reason to do this is because of the limitations of the "switch" statement. In fact I first learned this approach many years ago while working in C. The same technique might be more expressive in languages with a more dynamic approach to deciding flow based on aggregate data.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
COBOL of all languages has a neat syntax for this. Here's something from maybe 25 years ago that evaluates numeric expressions like a = b + 4. No order of precedence, just left to right but allowed nested parens.

Hmmm, looks like it also supported variables in a map-like table of name-value-pairs and functions like $name(arg,arg) that made calls to other COBOL programs.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic