Last active
August 1, 2016 10:39
-
-
Save qbit86/f124aac6a3638d1cbebdba47ba963ffe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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