Sunday, January 5, 2014

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.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Pong extends Applet implements Runnable, KeyListener{

    public void start ()
    {
        setSize(screen);
        setFocusable(true);
        Thread th = new Thread (this);
        th.start ();
    }

    public void stop()
    {
    }
    //Etc..
}
share|improve this question
add comment

4 Answers

up vote15down voteaccepted
You can simulate a basic FSM (Finite State Machine) using enums:
public enum State {

    ONE {
        @Override
        public Set<State> possibleFollowUps() {
            return EnumSet.of(TWO, THREE);
        }
    },

    TWO {
        @Override
        public Set<State> possibleFollowUps() {
            return EnumSet.of(THREE);
        }
    },

    THREE // final state 

    ;
    public Set<State> possibleFollowUps() {
        return EnumSet.noneOf(State.class);
    }

}
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.
share|improve this answer
1 
NOTE: when overriding methods in specific enums, their class is not the same. (in this case THREE.getClass() is State, and TWO.getClass() could be State$2) –  Asaf Nov 23 '11 at 10:52
1 
This code enlightens your tough process especially in java. Thanks. –  Trying Jul 3 '13 at 14:20
 
@Sean: Noting Asaf's comment, I would like to know, if you would see any disadvantage passing the possible follow up states in a constructor to the different states instead of the overwritten methods? (likeONE(EnumSet.of(TWO, THREE)), TWO(EnumSet.of(THREE))... –  Tom Fink Jul 17 '13 at 12:34
1 
Ok, figured it just out myself. Values of the enum obviously are not allowed inside constructors of the enum itself. :p –  Tom Fink Jul 17 '13 at 16:21
 
@TomFink exactly. otherwise your version would of course be preferable –  Sean Patrick Floyd Jul 17 '13 at 19:15
add comment
There is a design pattern for this situation. State Pattern and here is simple implementation in java.
share|improve this answer
 
So in my Pong app, how would I switch between the menu and gameplay state? –  Sam152 Apr 26 '11 at 12:42
 
the implementation mentioned above is not for the state pattern. Downvoting. –  javadeveloper Dec 20 '13 at 16:13
add comment
These links may help you:
share|improve this answer
1 
I could have searched for these, none of them really help me. I would just like a little code sample of how to achieve something really basic. –  Sam152 Apr 26 '11 at 12:40
add comment
this could help
share|improve this answer
add comment

No comments: