Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
State design pattern example
namespace DesignPatterns
{
/// <summary>
/// Default State that every state inherits from.
/// </summary>
public abstract class State
{
/// <summary>
/// Holds the current state we're in.
/// </summary>
public State CurrentState
{
get;
set;
}
public virtual string SitDown(State context)
{
return "";
}
public virtual string Walk(State context)
{
return "";
}
public virtual string Run(State context)
{
return "";
}
}
public class SittingState : State
{
public override string SitDown(State context)
{
context.CurrentState = new SittingState();
return "Sitting down.";
}
public override string Walk(State context)
{
context.CurrentState = new WalkingState();
return "I'm walking.";
}
public override string Run(State context)
{
return "I can't run while I'm sitting down.";
}
}
// (WalkingState and RunningState classes have been cut out, see the download)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.