Hi all.

In the course of my work i was in the need of a State Machine. i decided to implement a Generic version of the State Machine so I can reuse it if i will need it again. i started with defining the needs of a basic state and i can up with the following interface called IState and it contains the following members:

  • Onenter()
  • OnExit()
  • ChangeState(object Var)

OnEnter() is called before we enter the state, OnExit() i called before we exit the state and ChangeState(object Var) is called in order to switch states, notice that we pass an object as a variable to allow maximum flexibility. there is also a property called Id which represents the Id of the state. Again for flexibility Id is of type object. IState Implementation:

public interface IState

{

    /// <summary>

    /// the Id of the step.

    /// </summary>     
object Id

    {

        get;

        set;

    }
    /// <summary>

    /// This function will be called before entering the state.

    /// </summary>

    void OnEnter();

/// <summary>

    /// This function will be called before exiting the state.

    /// </summary>

    void OnExit();

/// <summary>

    /// This function will be called whenever there will be a need to change state

    /// </summary>

    object ChangeState(object Var);

}

Next implement the BaseState Class which implements the IState Interface. all t our costume states will have to be derived of the BaseState Class and provide implementation of the IState interface methods. BaseState implementation

public abstract class BaseState : IState

{

    private object m_Id;
    #region IState Members
// Should be implemented in the derived class

public abstract void OnEnter();

// Should be implemented in the derived class

    public abstract void OnExit();

// Should be Overidden in the derived class

    public abstract object ChangeState(object Var);

    #endregion
 

    #region IState Members     public object Id


    {

        get

        {

            return m_Id;

        }

        set

        {

            m_Id = value;

        }

    }

    #endregion

}

Now that we took care of the states we implement the BaseStateMachine Class. BaseStateMachine has a CurrentState of type IState and a Dictionary<object, IState>, again the Key is of type object to allow flexibility. there are also 2 methods: void AddState(IState NewState) to add the state in the initialization and IState GetState(object Id) to get the state when given the Id. BaseStateMachine implementation:

public class BaseStateMachine

{

    /// <summary>

    /// the current state of the state machine

    /// </summary>

    private IState m_CurrentState;    /// <summary>

    /// the list of all the states in the State machine

    /// </summary>

    private Dictionary<object, IState> m_States = new Dictionary<object, IState>();
    public IState CurrentState

    {

        get { return m_CurrentState; }

        set { m_CurrentState = value; }

    }

     /// <summary>

    /// Adds a state to the list of states in the state machine.

    /// </summary>

    /// <param name="NewState"></param>

    protected void AddState(IState NewState)

    {

        m_States.Add(NewState.Id, NewState);

    }    /// <summary>

    /// Gets the state that is represented by the Id

    /// </summary>

    /// <param name="Id"></param>

    /// <returns></returns>

    protected IState GetState(object Id)

    {

        return m_States[Id];

    }

}

Basically that’s all!! All you need to do is just to implement a class that derives from BaseStateMachine Class and implement the states you need which will all derive from BaseState Class.

Subscribe to our feed and get the complete code sample for this article

The example is of a simple state machine that changes states to Odd and Even state according to an Odd or Even input number.

Enjoy

Amit

Tags :

2 Responses to “How to Implement a Generic State Machine”


  1. Ray Clayton

    Said on February 4, 2010 :

    I get your feed but cannot find this article “How to Implement a Generic State Machine” to get the code sample.

1 Trackback(s)

  1. Mar 1, 2008: My Blog Stats - February 2008 | Dev102.com

Post a Comment