OO Design: Algorithm changing State (Information Flow Question)

  softwareengineering

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?

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)
{
    algorithm.Execute([&context](){context.switchToState(*something*)});
}

Where algorithm.Execute looks like:

template<typename CallbackT>
void Algorithm::Execute(CallbackT switchStateCallback)
{
    *code*
    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.

2

LEAVE A COMMENT