Created
May 9, 2016 13:11
-
-
Save KentaYamada/d6af908a2102e6a0830afead399c3ea1 to your computer and use it in GitHub Desktop.
State Pattern...??
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
| 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(); | |
| } | |
| } | |
| } |
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
| 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