Newbie questions

80 views
Skip to first unread message

David Kalkwarf

unread,
May 28, 2015, 5:38:10 PM5/28/15
to dotnet-state-ma...@googlegroups.com
I wish to implement the State pattern described in the Design Patterns book (Gamma et al).  At first, I thought that Stateless would be the answer, but after some examination it doesn't appear to be. 

In short, I expected each state to be encapsulated in its own class.  This doesn't seem to be the approach of Stateless.  Stateless seems to put the logic for all states into one class.  Am I missing something or is this the case?  With this approach it seems that all the work is done in the OnEntry and OnExit methods.  Also, it seems that I will have to use if/else in some places to determine whether to handle an action.

Any comments are welcome.

Dave

unread,
May 29, 2015, 10:05:51 AM5/29/15
to dotnet-state-ma...@googlegroups.com
Hi David, I'm not sure why you expect, or would need each state to be its own class.  At least the way I see it, the state machine could either be used to define complex behavior within a class, or could be passed objects that require interaction with each state.  I primarily handle the work in OnEntry.  No matter what, at some point you will need to use if/else to determine what to do next, i.e. what transition to make.  However, you don't rely on those control statements to handle actions.  Your decision to transition to a specific state from another state is handled when you define the transitions using methods like Permit.

The biggest issue you might encounter is that if you have a state machine that defines a long-running process, you will probably need a lot of stack space.  There are ways around this if it becomes a problem.

David Kalkwarf

unread,
May 29, 2015, 11:48:16 AM5/29/15
to dotnet-state-ma...@googlegroups.com
Putting each state in its own class has many advantages.  I suggest you read one of the many descriptions of the Gang of Four's (Gamma et al) State pattern. 

Grant BlahaErath

unread,
May 29, 2015, 8:10:24 PM5/29/15
to dotnet-state-ma...@googlegroups.com

Hi David,

Stateless is not an implementation of the GoF State pattern, its a library for implementing finite state machines.   That said, if the control logic of the handle method that is governing the State object gets complicated, the FSM implementation provided by Stateless can be easier to implement and maintain.

For my own use, I tend to use Stateless when my branch logic written in if/then/else statements starts to go beyond 4 branches. 

-g

--

---
You received this message because you are subscribed to the Google Groups "Stateless .NET State Machine Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotnet-state-machine-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ray Booysen

unread,
May 29, 2015, 8:24:10 PM5/29/15
to dotnet-state-ma...@googlegroups.com
Without sounding difficult, what benefits are you looking for by having them in separate classes.  Especially since you asked for help around this. :)

Dave

unread,
May 30, 2015, 1:27:20 PM5/30/15
to dotnet-state-ma...@googlegroups.com
It has been a while since I read GoF, so this was a good opportunity to take a look.  I agree with Grant, the State pattern in GoF is not a state machine.

I use Stateless to implement complex error handling behavior in machine control applications.  It's great.
Reply all
Reply to author
Forward
0 new messages