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?