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 ()
        Thread th = new Thread (this);
        th.start ();

    public void stop()
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 {
        public Set<State> possibleFollowUps() {
            return EnumSet.of(TWO, THREE);

    TWO {
        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
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
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
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
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


oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes

Zheng junxai5 said...

nike roshe run
coach outlet
adidas yeezy
kobe shoes 11
louis vuitton bags
red bottom shoes
adidas running shoes
cheap jordan shoes
coach factory outlet
louis vuitton outlet
michael kors handbags
louis vuitton handbags
michael kors outlet
adidas outlet store
retro 11
nike outlet
adidas yeezy
coach outlet online
ray bans
coach factory outlet online
hermes birkin handbags
insanity workout
adidas wings
toms shoes
air jordan shoes
nike trainers women
burberry handbags
ralph lauren outlet
oakley sunglasses wholesale
nike roshe one
cheap rolex watches
louis vuitton outlet
michael kors outlet clearance
cheap ray ban sunglasses
nike store
cheap jerseys
adidas factory outlet
tiffany and co
ray ban outlet

Walatra Gamat Emas Kapsul said...

This article is interesting and useful. Thank you for sharing. And let me share an article about health that God willing will be very useful. Thank you :)

Obat Migrain Tradisional
Penyebab Telat Haid dan Solusinya
Cara Menghilangkan Benjolan di Kelopak Mata
Cara Mengobati Kencing Tersendat
Obat Tradisional Telinga Berdengung
Cara Mengobati Prurigo