8
4
|
I am currently learning java and would like to know how to control state in a OO way. I implemented a Pong app. If I wanted multiple states like gameplay and menu, and each one of these states had to execute start, stop and run how would I achieve this and how would I switch between these states.
I know I could simply throw in a big switch statement but what's the best way to implement this?
I want to be able to switch to the menu state in the gameplay state and vice versa.
| |||
add comment |
15
|
You can simulate a basic FSM (Finite State Machine) using enums:
While the code to generate this will be very verbose if things get more complicated, the nice part is that you get compile-time safety, thread-safety and high performance.
| |||
add comment |
2
|
There is a design pattern for this situation. State Pattern and here is simple implementation in java.
| ||||||||
|
1
|
These links may help you:
| ||||
|
0
|
Try this link http://doc.akka.io/docs/akka/2.0.3/java/fsm.html
this could help
| ||
add comment |
ONE(EnumSet.of(TWO, THREE)), TWO(EnumSet.of(THREE))
... – Tom Fink Jul 17 '13 at 12:34