According to the "gang of four", the State design pattern is applicable when an object's behaviour depends on its state and it must change behaviour at run time, depending on that state.
The description of your state machine, which changes state based on an event, seems to match this description.
class StateMachine { // state machine that processes events received
State *s;
public:
StateMachine(); // constructor: here you shall set initial state
void process (Event* e); // processes the event and sets the next state
bool isReady (); // example of other requests
};
The principle of this pattern is to have an abstract base class, defining all the potential states dependent "actions" that StateMachine may delegate to a state.
class State {
public:
virtual void process (StateMachine *m, Event* e)=0; // called by state machine / pure virtual MUST be implemented in concrete state
virtual bool isReady(StateMachine *m); // example
... // other "actions"
};
The interface between your state and the state machine would then be easy to define. For example:
void StateMachine::Process (Event *e) {
s->Process (this, e); // calls the Process() of the current state
}
Derived concrete states shall then implement the different behaviours. As this *pattern works with virtual functions in C++, all the actions must be defined for each concrete state (either in base class or in the derived). This is a direct consequence of defining virtual functions.
But for actions that are not relevant for all the states, you may have a default action, either doing nothing, or triggering an error (error message, exception, ...). It depends on your general design, whether you prevent such situations to happen or not.
For example:
bool State::isReady(StateMachine *m) { // if this request is only relevant for a couple of states
throw std::exception ("unauthorized request"); // trigger an exception if it happens
return false;
} // this is done by default, except for concrete state that defines somtehing else
According to your description, I would really go for the state pattern.
S1and you receive the unhandled eventE62? - Bill Lynch