Finite State Machine and inter-FSM signaling

前端 未结 1 1457
栀梦
栀梦 2021-02-13 06:50

Recommendations for languages with native (so no FSM generation tools) support for state machine development and execution and passing of messages/signals. This is for

相关标签:
1条回答
  • 2021-02-13 07:19

    I agree that switch statements should be out of the question... they eventually lead to maintenance nightmares. Can't you use the State Pattern to implement your FSM? Depending on your actual implementation, you could use actors (if you have multiple FSM collaborating - hm... is that possible?). The nice thing about actors is that the framework for passing messages is already there.

    An example of using State would be:

    trait State {
      def changeState(message: Any): State
    }
    
    trait FSM extends Actor {
      var state: State
    
      def processMessage(message: Any) {
        state = state.changeState(message)
      }
    
      override def act() {
        loop {
          react {
            case m: Any => processMessage(m)
          }
        }
      }
    }
    

    This is very basic code, but as I don't know more of the requirements, that's the most I can think of. The advantage of State is that every state is self-contained in one class.

    0 讨论(0)
提交回复
热议问题