using System.Collections.Generic;
static void Main(string[] args)
PhaseState p = new PhaseState();
Console.WriteLine("Current State = " + p.CurrentState);
Console.WriteLine("Command.Begin: Current State = " + p.MoveNext(Command.Begin));
Console.WriteLine("Command.Pause: Current State = " + p.MoveNext(Command.Pause));
Console.WriteLine("Command.End: Current State = " + p.MoveNext(Command.End));
Console.WriteLine("Command.Exit: Current State = " + p.MoveNext(Command.Exit));
Console.WriteLine("Rewind:");
for (int i = 0; i < 3; i++)
Console.WriteLine("Advance:");
for (int i = 0; i < 10; i++)
Console.WriteLine("Rewind:");
for (int i = 0; i < 5; i++)
readonly State CurrentState;
readonly Command Command;
public StateTransition(State currentState, Command command)
CurrentState = currentState;
public override int GetHashCode()
return 17 + 31 * CurrentState.GetHashCode() + 31 * Command.GetHashCode();
public override bool Equals(object obj)
StateTransition other = obj as StateTransition;
return other != null && this.CurrentState == other.CurrentState && this.Command == other.Command;
Dictionary<StateTransition, State> transitions;
public State CurrentState { get; private set; }
CurrentState = State.Inactive;
transitions = new Dictionary<StateTransition, State>
{ new StateTransition(State.Inactive, Command.Exit), State.Terminated },
{ new StateTransition(State.Inactive, Command.Begin), State.Active },
{ new StateTransition(State.Active, Command.End), State.Inactive },
{ new StateTransition(State.Active, Command.Pause), State.Paused },
{ new StateTransition(State.Paused, Command.End), State.Inactive },
{ new StateTransition(State.Paused, Command.Resume), State.Active }
public State GetNext(Command command)
StateTransition transition = new StateTransition(CurrentState, command);
if (!transitions.TryGetValue(transition, out nextState))
throw new Exception("Invalid transition: " + CurrentState + " -> " + command);
public State MoveNext(Command command)
CurrentState = GetNext(command);
public static State Inactivate()
public static State Activate()