简体   繁体   中英

Transfering data between different states

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM