I have a class Context, which contains an instance of an abstract class State, which has multiple concrete subclasses (this is an implementation of the State Pattern).

Each State subclass executes a number of algorithms, which are themselves classes, and according to the outcome of an algorithm the state in the Context class might change.

My question is how should the algorithm make this state change in the Context class?

1) A simple solution would be to pass the Context object to the algorithm, but I don’t like that the algorithm know anything about the current State / Context.

2) I could write algorithm-specific wrapper classes of the Context class, where each wrapper has only one public function, namely to set the state of the Context according to the algorithm outcome.

3) Pass a function of Context which does a specific State change as function pointer to the algorithm and execute it when the state of Context should be changed.

While 1) is in my opinion the least favorable option, 2) would introduce a lot of new classes and I am not sure to what extent 3) is doable (in C++) and how Object Oriented this is.

What is the textbook way of dealing with this situation?

1 Answer 1


If the logic for choosing the next state is a part of the Algorithm classes, then clearly they must be able to communicate the specific state to change to to the Context class.

If the concept of states to change to is useless without the 'context' object (hard to tell), passing a reference to the context object should be fine - the algorithms and the context depend on each other and theres nothing you can do about it, so better make it as explicit as possible.

If algorithms dont really care if the state changes happen or not or what state is transitioned to (e.g. the logic for this resides entirely in the particular State subclass), you could pass a functor to the algorithm to be executed at the correct time, and the State subclass then implements the desired behavior (switching to a specific state). You can do this using either an std::function, or if possible, by passing a lambda as a template parameter to a method of the algorithm class (which should be more performant since it can be inlined - std::function may even do a heap allocation).

So in a State subclass you might have something like:

void State::ExecuteAlgorithms(Context& context)

Where algorithm.Execute looks like:

template<typename CallbackT>
void Algorithm::Execute(CallbackT switchStateCallback)

Of course you might want to add more than one callback, or pass parameters to the callback, to allow for more complex logic for choosing the next state.

You can replace the lambda with a class (which is how its implemented internally anyways) if you dont have them available, or use std::function instead of a template if the algorithm isn't just a single method or if you dont want templates.

  • I am just curious to what extend using a function pointer is still considered good OO
    – user695652
    Jun 19, 2015 at 18:45
  • Take the reasons why OO is good, and see if they apply to the chosen approach. Using std::function or template parameter is better than raw function pointer IMO, since it works for any type that 'implements' the 'callable' interface (eg an object with operator(), or a raw func pointer, or std::function...). A raw func pointer would not allow calling a class member function without workarounds, for instance.
    – Waterlimon
    Jun 19, 2015 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.