How to use this

State pattern

A state pattern is very useful when we need to handle complex behavior of a system. In a state pattern we are interested in state itself but also in transitions.

state.gif

First create some states:
  public class IdleState : State
  {
    public override void OnExitState()
    {
      // Code that will be executed when switching from this to another
    }

    public override void OnEnterState(object parameter)
    {
      // Code that will be executed when switching another to this one
    }
  }

  public class InvalidState : State
  {
    // implementation
  }

  public class RunningState : State
  {
    // implementation
  }

  public class StopState : State
  {
    // implementation
  }

Then create an instance of a StateManager with a StateManagerBuilder:
      var stateManager = new StateManagerBuild()
        .RegisterState(() => new IdleState())
        .RegisterState(() => new RunningState())
        .RegisterState(() => new InvalidState())
        .RegisterState(() => new StopState())
        .Build();

Finally all you have to do is to start the state manage by setting the first state:
      stateManager.ChangeState<IdleState>();

Last edited Mar 25, 2013 at 11:22 PM by decarufe, version 7

Comments

No comments yet.