#include using namespace std; typedef const char * String; class Context; class State; class ConcreteState1; class ConcreteState2; class State { public: virtual void Handle(Context& context)=0; virtual String getName()=0; }; class Context { State *state; public: static ConcreteState1 state1; static ConcreteState2 state2; Context(State *state):state(state) { } State* getState() { return state; } setState(State *state){ this->state = state; cout<<"State: "<getName()<Handle(*this); } }; class ConcreteState1 : public State { public: void Handle(Context& context) { context.setState((State*)&Context::state2); } String getName(){ return "State1";} }; class ConcreteState2 : public State { public: void Handle(Context& context) { context.setState((State*)&Context::state1); } String getName(){ return "State2";} }; ConcreteState1 Context::state1; ConcreteState2 Context::state2; int main() { Context c(&Context::state1); c.Request(); c.Request(); c.Request(); c.Request(); return 0; }