Skip to content

Instantly share code, notes, and snippets.

@tolland
Created September 11, 2016 22:15
Show Gist options
  • Save tolland/81a8114c2ceee4f5be3452d1a7b37c4e to your computer and use it in GitHub Desktop.
Save tolland/81a8114c2ceee4f5be3452d1a7b37c4e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
public class EventStateManager
{
IStateMachine currentState;
public EventStateManager()
{
//set the initial state @todo accessor?
currentState = MyChildSingletonA.Instance;
}
//in the state patten, this is used to execute the current state
//not sure if this is relevant to this system???
public void Update()
{
currentState.Execute(this);
}
public void ChangeState(IStateMachine newState)
{
//probably want to throw exception instead
if ((currentState == null) || (newState == null))
return;
currentState.Exit(this);
currentState = newState;
currentState.Entry(this);
}
static void Main(string[] args)
{
EventStateManager esm = new EventStateManager();
esm.ChangeState(MyChildSingletonB.Instance);
esm.ChangeState(MyChildSingletonA.Instance);
esm.ChangeState(MyChildSingletonB.Instance);
Console.ReadKey();
}
}
public class SingletonBase<T> where T : SingletonBase<T>, new()
{
private static T _instance = new T();
public static T Instance
{
get
{
return _instance;
}
}
}
public interface IStateMachine
{
void Entry(EventStateManager esm);
void Execute(EventStateManager esm);
void Exit(EventStateManager esm);
}
public class MyChildSingletonA : SingletonBase<MyChildSingletonA>, IStateMachine
{
public void Entry(EventStateManager esm)
{
Console.WriteLine("entry in here");
}
public void Execute(EventStateManager esm)
{
Console.WriteLine("executing in here");
}
public void Exit(EventStateManager esm)
{
Console.WriteLine("exiting in here");
}
}
public class MyChildSingletonB : SingletonBase<MyChildSingletonB>, IStateMachine
{
public void Entry(EventStateManager esm)
{
Console.WriteLine("entry - the other one");
}
public void Execute(EventStateManager esm)
{
Console.WriteLine("executing the other one");
}
public void Exit(EventStateManager esm)
{
Console.WriteLine("exiting the other one");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment