1

I have a state machine that will collect/generate data at different states and the data will then be used in some later state again. How do I transfer the data between states?

  1. Use a singleton object to hold the data
  2. Pass the data from state to state until it is no longer needed.

Both options seem ugly to me, is there a better one? Or maybe I should use another pattern instead of a state machine?

Motivation: The problem I'm trying to solve is to process a long text file where each line starts with a numeric id followed by data.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
Roland
  • 7,525
  • 13
  • 61
  • 124

1 Answers1

1

You can use Java Enum to do this. Basically, you may pass a state and a symbol (or event) to the new state. And in that case declaring states and symbols as enums will solve your problem by making it type-safe and easy too. The below link will help you.

https://stackoverflow.com/a/13224709/5883136

https://www.baeldung.com/java-enum-simple-state-machine