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 890941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:48:49+00:00 2026-05-15T13:48:49+00:00

I have a C++ state machine implemented using the State design pattern. Each state

  • 0

I have a C++ state machine implemented using the State design pattern. Each state is implemented as a nested friend class of the context class.

class Context {
public:
  /* The Context class' user calls process1() to get it to perform an action */
  void process1();

private:
  class IState;

  void switchState( IState *newState );

  class IState {
    virtual void doProcess( Context &context ) = 0;
  };

  class StateA : public Context::IState {
    void doProcess( Context &context );
  };
  friend class StateA;

  class StateB : public Context::IState {
    void doProcess( Context &context );
  };
  friend class StateB;
  .
  .
  .
  class StateJ : public Context::IState {
    void doProcess( Context &context );
  };
  friend class StateJ;
};

Currently, a successful iteration of the state machine runs from Context::StateA to Context::StateJ when Context::process1() is called but some of the states contain internal logic to determine whether to loop back to an earlier state. So a typical execution will look like:

StateA
StateB
StateC
StateD
StateE
StateC
StateD
StateE
StateF
StateG
StateH
StateI
StateJ

The internal logic to determine next state is currently being implemented by the respective state itself by storing data in the context object. What I need to do now is add a Context::process2() option that significantly differs in the order of execution of states. Of course, this can be done using flags that are set in the context object but I was wondering if there is better way to implement this; maybe even use this method to rewrite the handling of state switches within Context::process2().

The Visitor design pattern might do the trick but I’m not sure whether its intended for implementing state machines. When using Visitor, process1() could contain all logic for the order of execution of states and then just call each state in that order. Similarly, process2() would handle all of its own logic.

EDIT:
To those of you that replied saying I should be creating a separate state machine, the reason I’m looking to avoid that is because the code for the states used by the second state machine is identical to those in the first; only the progression is different.

The second state machine will go through the following state transitions:

StateA
StateB
StateC
StateJ

So I’m trying to eliminate duplicated code.

  • 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. Editorial Team
    Editorial Team
    2026-05-15T13:48:49+00:00Added an answer on May 15, 2026 at 1:48 pm

    I assume that in your code, switchState is invoked by the individual states when it’s time to transition to something else. Something like this:

    void StateA::doProcess(Context& context) {
       context.switchState(new StateB()); // NOTE: potential leak!
    }
    

    Is this the case?

    If so, one thing you might consider is having the states return transition objects that abstractly represent a control point in your state graph. You then have the Context run a loop which executes the states, retrieves the resulting transitions, and maps the transitions to the appropriate next states for whatever process you have. The transition map can be set up differently for each process method that you have.

    Pros:

    • States don’t have to know about each other. The transition is their declaration to the world of what they’ve done. It’s up to the context to route between the states in appropriate ways. This could make the states reusable across different Context objects, for example.
    • Allows the idea of “same structure, slightly different behavior” by allowing the context to essentially maintain the same state graph, but plug in a slightly different state with a compatible transition interface as the target of a particular transition.

    Cons:

    • Adds an extra layer of stuff to set up. Transition mapping code is mostly boilerplate. Tools to generate the boilerplate may help.
    • Doesn’t help you if you really want to do something radically different in the two processes which the states don’t have the right transitions to support – but then, you’re trying to reuse at the wrong level – you probably want different state objects at that point.

    EDIT: Sample code is up at http://pastebin.com/eBauP060 .

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

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Instead of cramming all into a one-line lambda, I prefer… May 15, 2026 at 3:06 pm
  • Editorial Team
    Editorial Team added an answer If the association between tblContacts and tblStates is properly set… May 15, 2026 at 3:06 pm
  • Editorial Team
    Editorial Team added an answer I asked a similar question, looks like you'll have to… May 15, 2026 at 3:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.