Hi
Here's a very simple FSM implementation:
public delegate void ProcessEvent<TEvent>(TEvent ev);
public abstract class StateMachine<TEvent>
{
private ProcessEvent<TEvent> state;
protected ProcessEvent<TEvent> State
{
get { return this.state; }
set { this.state = value; }
}
public void ProcessEvent(TEvent ev)
{
this.state(ev);
}
}
You would use it as follows:
public class MyFsm : StateMachine<byte>
{
public MyFsm()
{
this.State = this.Started;
}
private void Started(byte ev)
{
Console.WriteLine(ev);
if (ev == 255)
{
this.State = this.Stopped;
}
}
private void Stopped(byte ev) { }
}
class Program
{
static void Main(string[] args)
{
MyFsm fsm = new MyFsm();
fsm.ProcessEvent((byte) 0);
fsm.ProcessEvent((byte) 255);
fsm.ProcessEvent((byte) 0);
}
}