Click here to Skip to main content
11,702,168 members (62,738 online)
Click here to Skip to main content

Another Generic State Machine

, 19 May 2013 CPOL 10.1K 7
Rate this:
Please Sign up or sign in to vote.
A basic yet generic state machine implementation

Introduction

I was looking for a generic implementation of the State Machine pattern. Of course, there are many out there. But what I found was either too specific or part of some monster framework. So I wrote my own.

Background

The State Machine pattern is discussed here.

Using the Code

To use the StateMachine, simply derive from the class and setup your states and transition events:

public class MyStateMachine : StateMachine<StateData, int, string>
{
   public MyStateMachine ()
   {
       AddState(new State { data = new StateData { Identity = 0, Name = "Solid" }, 
                            IdSelector = x=> x.Identity });
       AddState(new State { data = new StateData { Identity = 1, Name = "Liquid" }, 
                            IdSelector = x => x.Identity });
       AddState(new State { data = new StateData { Identity = 2, Name = "Gas" }, 
                            IdSelector = x => x.Identity });
       AddEvent(new StateMachine<StateData, int, string>.Event { ID = "Cool", 
                   Transitions = new Dictionary<int, int> { { 0, 0 }, { 1, 0 }, { 2, 1 } } });
       AddEvent(new StateMachine<StateData, int, string>.Event { ID = "Heat", 
                   Transitions = new Dictionary<int, int> { { 0, 1 }, { 1, 2 }, { 2, 2 } } });
   } 
   public void Heat()
   {
       var ev = GetEventByKey("Heat");
       FireEvent(ev);
   }
   public void Cool()
   {
      var ev = GetEventByKey("Cool");
      FireEvent(ev);
   }

} 

An event in this StateMachine is the action that happens in the world, this then triggers transitions in the state machine from one state to another. You can setup public methods to match the actions set on your StateMachine.

In this simplified example, the state machine matches the state of matter depending on its heat property. Cooling a gas will result in a liquid. Heating a solid will result in a liquid.

Using the state machine is easy:

var target = new MyStateMachine();
target.LeaveState += LeaveHandler; 
target.SetState(1); 
target.SetState(1); 

Points of Interest

I ran into interesting questions while writing unit tests for this class. See tip on "Mocking Event Handlers".

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Yossi Yaari
Software Developer (Senior)
Israel Israel
I've been developing in C# for over 7 years.
Always in some unique corner or another.

You may also be interested in...

Comments and Discussions

 
QuestionI am missing the source Pin
manfbraun8-Jul-13 9:12
membermanfbraun8-Jul-13 9:12 
AnswerRe: I am missing the source Pin
manfbraun8-Jul-13 9:16
membermanfbraun8-Jul-13 9:16 
GeneralRe: I am missing the source Pin
Yossi Yaari8-Jul-13 18:29
memberYossi Yaari8-Jul-13 18:29 
QuestionWhat is state machine Pin
Tridip Bhattacharjee18-May-13 8:22
memberTridip Bhattacharjee18-May-13 8:22 
AnswerRe: What is state machine Pin
forester joe18-May-13 9:57
memberforester joe18-May-13 9:57 
QuestionSource Code ? Pin
Member 229921117-May-13 6:19
memberMember 229921117-May-13 6:19 
AnswerRe: Source Code ? Pin
forester joe18-May-13 7:56
memberforester joe18-May-13 7:56 
GeneralRe: Source Code ? Pin
Member 229921120-May-13 6:17
memberMember 229921120-May-13 6:17 
AnswerRe: Source Code ? Pin
Yossi Yaari20-May-13 9:38
memberYossi Yaari20-May-13 9:38 
GeneralRe: Source Code ? Pin
Member 229921120-May-13 10:12
memberMember 229921120-May-13 10:12 
QuestionGreat design ! Pin
Paul Van Bladel II11-May-13 20:22
memberPaul Van Bladel II11-May-13 20:22 
AnswerRe: Great design ! Pin
forester joe18-May-13 7:56
memberforester joe18-May-13 7:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 19 May 2013
Article Copyright 2013 by Yossi Yaari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid