Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created May 9, 2016 13:11
Show Gist options
  • Select an option

  • Save KentaYamada/d6af908a2102e6a0830afead399c3ea1 to your computer and use it in GitHub Desktop.

Select an option

Save KentaYamada/d6af908a2102e6a0830afead399c3ea1 to your computer and use it in GitHub Desktop.
State Pattern...??
using System;
namespace StatePattern
{
class Program
{
static void Main(string[] args)
{
IState state = new StateA(); //Start state
string target = "D"; //Target state
//Search state
while (state != null && target != state.StateName)
{
state = state.ChangeState();
}
//Assert
if (state == null)
{
Console.WriteLine("Target state not supported.");
}
else
{
Console.WriteLine("Get state:{0}", state.StateName);
}
Console.ReadKey();
}
}
}
using System;
namespace StatePattern
{
interface IState
{
string StateName { get; }
IState ChangeState();
}
class StateA : IState
{
public StateA()
{ }
public string StateName { get { return "A"; } }
public IState ChangeState()
{
Console.WriteLine("Change state A to B.");
return new StateB();
}
}
class StateB : IState
{
public StateB()
{ }
public string StateName { get { return "B"; } }
public IState ChangeState()
{
Console.WriteLine("Change state B to C.");
return new StateC();
}
}
class StateC : IState
{
public StateC()
{ }
public string StateName { get { return "C"; } }
public IState ChangeState()
{
Console.WriteLine("StateC is end of state.");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment