Skip to content

Instantly share code, notes, and snippets.

@qbit86
Last active August 1, 2016 10:39
Show Gist options
  • Save qbit86/f124aac6a3638d1cbebdba47ba963ffe to your computer and use it in GitHub Desktop.
Save qbit86/f124aac6a3638d1cbebdba47ba963ffe to your computer and use it in GitHub Desktop.
namespace Machinery
{
abstract class StateBase<TInput>
{
public event EventHandler<TInput> InputRequested;
public virtual void Enter(StateBase<TInput> oldState, TInput input) { }
public virtual void Exit(StateBase<TInput> newState, TInput input) { }
public abstract ReactionBase<TInput> React(TInput input);
}
abstract class ReactionBase<TInput>
{
internal abstract StateBase<TInput> GetNewState();
}
sealed class DiscardReaction<TInput> : ReactionBase<TInput>
{
internal override StateBase<TInput> GetNewState() { return null; }
public static readonly DiscardReaction<TInput> Default = new DiscardReaction<TInput>();
}
sealed class TransitReaction<TState, TInput> : ReactionBase<TInput> where TState : StateBase<TInput>
{
public TransitReaction(StateBase<TInput> newState)
{
if (newState == null)
throw new ArgumentNullException(nameof(newState));
_newState = newState;
}
internal override StateBase<TInput> GetNewState() { return _newState; }
private readonly StateBase<TInput> _newState;
}
static class TransitReactionCompanion<TState, TInput> where TState : StateBase<TInput>, new()
{
public static readonly TransitReaction<TState, TInput> Default = new TransitReaction<TState, TInput>(new TState());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment