Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 110159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:10:59+00:00 2026-05-11T02:10:59+00:00

I have an application that has multiple states, with each state responding to input

  • 0

I have an application that has multiple states, with each state responding to input differently.

The initial implementation was done with a big switch statement, which I refactored using the state pattern (at least, I think it’s the state pattern. I’m kind of new to using design patterns, so I tend to get them confused) –

class App {    public:     static App * getInstance();     void addState(int state_id, AppState * state) { _states[state_id] = state; }     void setCurrentState(int state_id) { _current_state = _states[state_id]; }    private:     App()     ~App();     std::map<int, AppState *> _states;     AppState * _current_state;     static App * _instance; }  class AppState {    public:     virtual void handleInput() = 0;     virtual ~AppState();    protected:     AppState();  } 

Currently, each state is polling the OS for input, and acting accordingly. This means each concrete state has a huge switch statement with a case for each valid keypress. Some cases call functions, and other cases issue state changes by using App::setCurrentState(newstate). The catch is that a key that does something in one state may not do anything (or in rare circumstances, may do something different) in another state.

Okay, I think that’s the pertinent background. Here’s the actual question(s) –

First, what’s the best way to eliminate the huge switch statements in the concrete states? This question suggests the command pattern, but I don’t understand how I would use it here. Can someone help explain it, or suggest another solution?

As a side note, I’ve considered (and am not opposed to) the idea of letting the App class do the polling of the OS, and then pass inputs to _current_state->handleInput. In fact, something tells me that I’ll want to do this as part of the refactoring. I just haven’t done it yet.

Second, state changes are made by calling App::setCurrentState(newstate). I realize that this is akin to using globals, but I’m not sure of a better way to do it. My main goal is to be able to add states without modifying the App class. Suggestions would be welcome here as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:11:00+00:00Added an answer on May 11, 2026 at 2:11 am

    Given your refactoring, it looks like the question now is how to reduce the amount of keycode parsing code that will be duplicated across your various concrete AppState implementations. As you mention, this leads to multiple switch statements which select which code to call to handle the keystroke input.

    Depending on how performance critical this code is, you can separate that keycode decoding logic into a processInput(int keycode) method in App (or as a concrete method in AppState) and create a set of handle*Pressed() functions in your AppState classes. Depending on how many types of keystrokes you are looking at handling, this might be reasonable, or it might lead to far too many methods to implement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TreeView control in my WinForms .NET application that has multiple levels
I have an application that has many different types of objects that each persist
I have a window form application, and it has multiple threads running that would
We have a .NET application that has grown over time and now has multiple
We have an application that has to be flexible in how it displays it's
I have an application that has created a number of custom event log sources
I have an application that has Powershell 1 embedded into it, but we need
We have an application that has one or more text console windows that all
I have an application that has several objects (about 50 so far, but growing).
I have an application that has to deal with getting special characters in its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.