#ifndef _STATE_H_ #define _STATE_H_ #include #include class State { protected: virtual void run (std::string input, std::string& output)=0; virtual State* newState ()=0; friend class StateControl; }; /************************************ * Implementation details * ************************************ // Each state should be like: class SomeState : public State { // Singleton object static SomeState state; protected: // This is where all computation should be virtual void run (std::string input, std::string& output); // This is the method that knows when to change states virtual State* newState (); // Return new state or itself public: // Singleton cosntructor static SomeState* Instance(); SomeState (); }; // The StateControl should be like: class StateControl { std::string input; State* currentState; public: StateControl () { // There must be an initial state this->currentState = (State*) InitialState::Instance(); } bool setInput (std::string in) { // This could be a lot different by reading the input itself this->input = in; } void run (std::string& result) { // Run this->currentState->run(this->input, result); // And change state, if need to this->currentState = this->currentState->newState(); } }; */ #endif // _STATE_H_