Last active
August 29, 2022 12:42
-
-
Save eastmad/60601442500c07aab19d20762319f387 to your computer and use it in GitHub Desktop.
Simple State Machine for a cycle
This file contains 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 TheNewStack | |
{ | |
public enum SeasonalState { Spring, Summer, Autumn, Winter } | |
public class SimpleStateMachine | |
{ | |
public Season CurrentSeason; | |
public SimpleStateMachine() | |
{ | |
CurrentSeason = new Season(SeasonalState.Spring); | |
Season season = CurrentSeason.NextSeason = new Season(SeasonalState.Summer); | |
season = season.NextSeason = new Season(SeasonalState.Autumn); | |
season = season.NextSeason = new Season(SeasonalState.Winter); | |
season.NextSeason = CurrentSeason; | |
} | |
public Season Next() | |
{ | |
return CurrentSeason = CurrentSeason.NextSeason; | |
} | |
public class Season | |
{ | |
readonly SeasonalState seasonstate; | |
public Season NextSeason; | |
public Season(SeasonalState seasonstate) | |
{ | |
this.seasonstate = seasonstate; | |
} | |
public void Welcome() | |
{ | |
Console.WriteLine("This is " + seasonstate); | |
} | |
} | |
} | |
} | |
public class Program | |
{ | |
static void Main(String[] args) | |
{ | |
SimpleStateMachine s = new SimpleStateMachine(); | |
for (int i = 0; i < 6; i++) | |
{ | |
s.CurrentSeason.Welcome(); | |
s.Next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment