 // Purpose.  Factory Method             #include <iostream>
 //           creation via inheritance   using namespace std;
 //
 // Discussion.  The architect has done  class Stooge {
 // an admirable job of decoupling the   public:
 // client from Stooge concrete derived     virtual void slapStick() = 0;
 // classes, and, exercising polymor-    };
 // phism.  But there remains coupling
 // where instances are actually         class Factory {
 // created.  If we design an "extra     public:
 // level of indirection" (a "factory       // Factory Method (virtual ctor)
 // method") and have clients use it        static Stooge* create( int );
 // (instead of "new"), then the last    };
 // bit of coupling goes away.  The
 // "factory method" (aka "virtual       void main( void ) {
 // constructor") can be defined in the     Stooge*  roles[10];
 // Stooge base class, or, in a             int      in, j, i = 0;
 // separate "factory" class.  Note
 // that main() is no longer dependent      while (1) {
 // on Stooge derived classes.                 cout << "L(1) M(2) C(3) Go(0): ";
                                               cin >> in;
 #include <iostream>                           if ( ! in ) break;
 using namespace std;                          roles[i++] = Factory::create(in);
                                            }
 class Stooge { public:                     for (j=0; j < i; j++)
    virtual void slapStick() = 0;              roles[j]->slapStick();
 };                                         for (j=0; j < i; j++)
                                               delete roles[j];
 class Larry : public Stooge { public:   }
    void slapStick() {
       cout << "Larry: poke eyes"        class Larry : public Stooge { public:
          << endl; }                        void slapStick() {
 };                                            cout << "Larry: poke eyes"
 class Moe : public Stooge { public:              << endl; }
    void slapStick() {                   };
       cout << "Moe: slap head"          class Moe : public Stooge { public:
          << endl; }                        void slapStick() {
 };                                            cout << "Moe: slap head"
 class Curly : public Stooge { public:            << endl; }
    void slapStick() {                   };
       cout << "Curly: suffer abuse"     class Curly : public Stooge { public:
          << endl; }                        void slapStick() {
 };                                            cout << "Curly: suffer abuse"
                                                  << endl; }
 void main( void ) {                     };
    Stooge*  roles[10];
    int      in, j, i = 0;               Stooge* Factory::create( int in ) {
                                            if (in == 1)
    cout << "L(1) M(2) C(3) Go(0): ";          return new Larry;
    cin >> in;                              else if (in == 2)
    while (in) {                               return new Moe;
       if (in == 1)                         else
          roles[i++] = new Larry;              return new Curly;
       else if (in == 2)                 }
          roles[i++] = new Moe;
       else                              // L(1) M(2) C(3) Go(0): 1
          roles[i++] = new Curly;        // L(1) M(2) C(3) Go(0): 2
       cout << "L(1) M(2) C(3) Go(0): "; // L(1) M(2) C(3) Go(0): 3
       cin >> in;                        // L(1) M(2) C(3) Go(0): 1
    }                                    // L(1) M(2) C(3) Go(0): 0
    for (j=0; j < i; j++)                // Larry: poke eyes
       roles[j]->slapStick();            // Moe: slap head
    for (j=0; j < i; j++)                // Curly: suffer abuse
       delete roles[j];                  // Larry: poke eyes
 }

