 // Purpose.  State                      #include <iostream.h>
 //
 // Discussion.  The boss's behavior is  class Boss {
 // "morphing" radically as a function   public:
 // of his mood.  Operations have large     friend class Disposition;
 // "case" constructs that depend on        Boss();
 // this "state" attribute.  Like large     void decide();
 // procedures, large conditional stmts     void direct();
 // are undesirable.  They're monolith-  private:
 // ic, and tend to make maintenance        Disposition*  moods_[2];
 // very difficult.  The State pattern      int           current_;
 // models individual states with de-    };
 // rived classes of an inheritance
 // hierarchy, and front-ends this       class Disposition {
 // hierarchy with an "interface"        public:
 // object that knows its "current"         virtual void decide( Boss* ) = 0;
 // state.  This partitions and local-      virtual void direct( Boss* ) = 0;
 // izes all state-specific responsi-    protected:
 // bilities; allowing for a cleaner        void toggle( Boss* b ) {
 // implementation of dynamic behavior         b->current_ = ! b->current_; }
 // that must change as internal state   };
 // changes.
                                         class DilbertZone :
 class Boss {                                  public Disposition { public:
 public:                                    void decide( Boss* b ) {
    Boss() {                                   cout << "Eenie, meenie, mynie,";
       mood_ = DilbertZone;                    cout << " moe.\n";  toggle(b); }
    }                                       void direct( Boss* b ) {
    void decide() {                            cout << "My whim - you're";
       if (mood_ == DilbertZone) {             cout << " nightmare.\n";
          cout << "Eenie, meenie,";            toggle(b); }
          cout << " mynie, moe.\n";      };
       }                                 class Sunny :
       else if (mood_ == Sunny) {              public Disposition { public:
          cout << "You need it - you";      void decide( Boss* b ) {
          cout << " got it.\n";                cout << "You need it - you got";
       }                                       cout << " it.\n";  toggle(b); }
       toggle();                            void direct( Boss* b ) {
    }                                          cout << "Follow me.\n";
    void direct() {                            toggle(b); }
       if (mood_ == DilbertZone) {       };
          cout << "My whim - you're";
          cout << " nightmare.\n";       Boss::Boss() {
       }                                    moods_[0] = new DilbertZone;
       else if (mood_ == Sunny)             moods_[1] = new Sunny;
          cout << "Follow me.\n";           current_ = 0; }
       toggle();                         void Boss::decide() {
    }                                       moods_[current_]->decide( this ); }
 private:                                void Boss::direct() {
    enum Disposition { Sunny,               moods_[current_]->direct( this ); }
                       DilbertZone};
    Disposition  mood_;                  void main( void )
    void toggle() { mood_ = ! mood_; }   {
 };                                         Boss ph;
                                            for (int i=0; i < 2; i++) {
 void main( void )                             ph.decide();
 {                                             ph.decide();
    Boss ph;                                   ph.direct(); }
    for (int i=0; i < 2; i++)            }
    {                                    // Eenie, meenie, mynie, moe.
       ph.decide();                      // You need it - you got it.
       ph.decide();                      // My whim - you're nightmare.
       ph.direct();                      // You need it - you got it.
    }                                    // Eenie, meenie, mynie, moe.
 }                                       // Follow me.

