StateMachine in c# and xml

By ramon
September 28, 2005
1

Yesterday I browsed through the new articles at codeproject.com. Leslie Sanford StateMachine articles had my attention. He has designed and implemented it in c#. A job done pretty well I think. I took a look at his code that reads an xml definition to deserialize its state design. My thoughts were that it wasn’t really readable and logical so I made a class design that serializes to a "self-describing" xml document.

An example:
<?xml version="1.0"?>
<Machine Id="Switch" InitialState="On" xmlns="Exyll.StateMachine">
  <SubStates>
    <State Id="On">
      <Transitions>
        <Transition EventName="TurnOff" Target="Off" />
      </Transitions>
    </State>
    <State Id="Off">
      <Transitions>
        <Transition EventName="TurnOn" Target="On" />
      </Transitions>
    </State>
  </SubStates>
  <Events>
    <Event>TurnOff</Event>
    <Event>TurnOn</Event>
  </Events>
</Machine>

If you are interested in this class design and the xmlattributes I had to add to achieve this xml format then download StateMachine code.

Comments: 1

  1. Julian says:

    Hi Ramon,

    I found very interesting that you’ve done.

    I have a question.

    I want to implement functionality that give possibility switch between tabItems. (TabItem1,TabItem2,TabItem3….TabItemN), when user click button “next” on the Form. And for that reason I’d like to use State Machine Pattern and apply Singletone there.

    Probably I need to have following things: changeState, getNextState, previousState,currentState,nextState.
    Each state it’s a name of TabItem (for instance: Home Item, Welcome Item etc.).

    I understand that it shod be like this this this and this. But I can’t do that..because I’ve no enough experience 🙁

    Could you please help me with that, I’ll be very thankful. I really want to understand how it works in my example.

    Thank you in advance,

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta