Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active January 19, 2017 23:32
Show Gist options
  • Select an option

  • Save Porges/1dc626b8eacf01421f89df1851d5f6bd to your computer and use it in GitHub Desktop.

Select an option

Save Porges/1dc626b8eacf01421f89df1851d5f6bd to your computer and use it in GitHub Desktop.
abstract class State
{
public abstract bool IsFinalState { get; }
public abstract bool IsValidTransition(State next);
public abstract override string ToString();
public static State NotStarted { get; } = new NotStartedT();
public static State Running { get; } = new RunningT();
public static State Errored { get; } = new ErroredT();
public static State Completed { get; } = new CompletedT();
private abstract class FinalState : State
{
public override bool IsFinalState => true;
public override bool IsValidTransition(State next) => false;
}
private sealed class ErroredT : FinalState
{
public override string ToString() => "Errored";
}
private sealed class CompletedT : FinalState
{
public override string ToString() => "Completed";
}
private sealed class RunningT : State
{
public override bool IsFinalState => false;
public override bool IsValidTransition(State next)
=> next == Completed || next == Errored;
public override string ToString() => "Running";
}
private sealed class NotStartedT : State
{
public override bool IsFinalState => false;
public override bool IsValidTransition(State next)
=> next == Running || next == Errored;
public override string ToString() => "Not Started";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment