Design patten for application states

What exactly is the preferred design pattern for application states in Processing? I am coming from Java, but haven't done too much GUI. This is what I'm currently using for switching application states:

Main class:

void setup() {
    size(900, 500);
    setCurrentState(state.MAIN);
    //...
}
    AppState currentState;
void draw() {
    currentState.tick();
}
void setCurrentState(AppState as) {
    currentState = as;
    currentState.init();
    //...
}
class States {
    AppState LOADING = new LoadingScreen();
    AppState MAIN = new MainScreen();
}

Then, I have an abstract class AppState and some classes that extend it and override its methods:

class LoadingScreen extends AppState {
    //AtOverride
    public void tick() {
        //This method is called every frame while loading is the current app state
        //All logic goes here
    }
    //AtOverride
    public void init() {
        //This code is run every time the current app state is switched to loading
    }
}

Basically, I'm only calling tick() of the current app state instead of putting all my logic in draw(). Similarly, I can also make classes like this for main screen, settings menu and anything else I might need. Is this approach considered good or bad? If it's bad, what should I use then? I am new to Processing and any help will be very much appreciated!

Answers

Sign In or Register to comment.