Outcoldman's answer provides many great options.
, FSM , . . Action<T> generic:
public class Context
{
public Action<Context> State { get; internal set; }
public Context(Action<Context> state)
{
State = state;
}
public void Run()
{
while (State != null)
{
State(this);
}
}
}
" " :
public static class SimpleStateMachine
{
public static void StateA(Context context)
{
context.State = StateB;
}
public static void StateB(Context context)
{
Console.Write("Input state: ");
var input = Console.ReadLine();
context.State = input == "e" ? (Action<Context>)null : StateA;
}
}
, :
var context = new Context(SimpleStateMachine.StateA);
context.Run();
Console.Read();
, , , , :
Action<Context> process = context =>
{
context.State = nextContext =>
{
nextContext.State = null;
};
};