Don't Fear the State Machine

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Don't Fear the State Machine

A 10-minute lightning talk about applying Finite State Machines as a software design pattern

Cara Fonseca-Ensor

October 03, 2013

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

25 slides
  1. What if you accidentally call someone, and want to stop

    it? Yeah... we need to add a ‘Cancel’ button, don’t we?
  2. if (!callAnswered) { // replace incoming message with cancelled message

    } else if (!connected) { // stop connecting // replace connecting message with cancelled message }
  3. if (!callAnswered) { // replace incoming message with cancelled message

    } else if (!connected) { // stop connecting // replace connecting message with cancelled message } else { // disconnect // replace video streams with cancelled message }
  4. if ... else { // disconnect if (waitingForMedia && waitingForRemoteMedia)

    { } else if (waitingForMedia) { } else if (waitingForRemoteMedia) { } else { } }
  5. public class StateMachine { private StateMachineState state; public void CallRequestReceived()

    {...} public void CallAccepted() {...} public void CallCancelled() {...} public void MediaDialogOpened() {...} public void LocalStreamCreated() {...} public void RemoteStreamCreated() {...} public void DismissMessage() {...} public void EndCall() {...} }
  6. public class StateMachine { ... public void CallCancelled() { switch(state)

    { case StateMachineStates.Receiving: ... case StateMachineStates.Connecting: ... case StateMachineStates.ApprovingMedia: ... case StateMachineStates.WaitingForMedia: ... case StateMachineStates.WaitingForRemoteMedia: ... default: context.Warn("Undefined transition " + "'CallCancelled' from {0}", state); break; } } ... }
  7. public class StateMachine { private IStateMachineContext context; ... public void

    CallCancelled() { switch(state) { case StateMachineStates.Receiving: state = StateMachineStates.Cancelled; context.DisplayCancelledMessage(); break; case StateMachineStates.Connecting: state = StateMachineStates.Cancelled; context.StopConnecting(); break; case StateMachineStates.ApprovingMedia: state = StateMachineStates.Cancelled; context.Disconnect(); break; ... } } ... } public interface IStateMachineContext { void PlayPhoneSound(); void ConnectToSession(); void DisplayCancelledMessage(); void StopConnecting(); void DisplayPrompt(); void WaitForRemote(); void WaitForLocal(); void Disconnect(); void DisplayStreams(); void CloseCancelledMessage(); void CloseStreams(); }
  8. • UML Tutorial: Finite State Machines Robert C. Martin http://www.objectmentor.com/resources/articles/umlfsm.pdf

    • The State Machine Compiler Charles W. Rapp et al. http://smc.sourceforge.net/ • Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides • Game Programming Patterns Bob Nystrom http://gameprogrammingpatterns.com/state.html

Other Decks in Programming