#ifndef STATE_H #define STATE_H /////////////////////////////////////////////////////////////// // state.h - defines states in terms of the events // // that trigger transitions to another state // // // // Language: Visual C++, ver 5.0 // // Platform: Micron Dual Pentium 200, NT 4.0 // // Application: Design Patterns - property pattern, Su98 // // Author: Jim Fawcett, CST 2-187, (314) 443-3948 // // fawcett@neptune.syr.edu // /////////////////////////////////////////////////////////////// // Design notes: // uses monostate, state, and property patterns #include #include using namespace std; // an extensible enumeration of states const int numStates = 4; const int onFF = 0; const int goUp = 1; const int onSF = 2; const int goDn = 3; //----< define events, store state references >---------------- class ElevBase { public: virtual ElevBase* upButton() { return this; } virtual ElevBase* dnButton() { return this; } virtual ElevBase* arrived() { return this; } protected: static vector registry; // shared by all elevator states }; //----< factor makeStates function out of derived classes >---- template class ElevState : public ElevBase { public: ElevBase* makeState(int s) { if(s<0 || numStates<=s) { cout << " not a valid state\n"; exit(1); } ElevBase* pElevBase; return (pElevBase = registry[s] = new DerivedState); } }; // /////////////////////////////////////////////////////////////// // these are the property classes // /////////////////////////////////////////////////////////////// //----< on the first floor >----------------------------------- class onFirstFloor : public ElevState { public: ~onFirstFloor() { delete registry[onFF]; } ElevBase* upButton() { cout << " going up\n"; return registry[goUp]; } }; //----< going up to the second floor >------------------------- class goingUp : public ElevState { public: ~goingUp() { delete registry[goUp]; } ElevBase* arrived() { cout << " arrived on second floor\n"; return registry[onSF]; } }; //----< on the second floor >---------------------------------- class onSecondFloor : public ElevState { public: ~onSecondFloor() { delete registry[onSF]; } ElevBase* dnButton() { cout << " going down\n"; return registry[goDn]; } }; //----< going down to the first floor >------------------------ class goingDown : public ElevState { public: ~goingDown() { delete registry[goDn]; } ElevBase* arrived() { cout << " arrived on first floor\n"; return registry[onFF]; } }; #endif