 // Purpose.  Prototype                  #include <iostream.h>
 //           creation via delegation
 // Discussion.  The architect has done  class Stooge { public:
 // an admirable job of decoupling the      virtual Stooge* clone() = 0;
 // client from Stooge concrete derived     virtual void slapStick() = 0;
 // classes and exercising polymor-      };
 // phism.  But there remains coup-
 // ling where instances are actually    class Factory {
 // created.  If we design an "extra     public:
 // level of indirection" (a "factory")     static Stooge* create( int i );
 // and have clients use it (instead of  private:
 // "new"), then the last bit of coup-      static Stooge* prototypes_[4];
 // ling goes away.  The Prototype       };
 // pattern suggests delegating the
 // creation service to contained ob-    void main( void )
 // jects that know how to "clone"       {
 // themselves.  This strategy also         Stooge*  roles[10];
 // allows us to retire the "case"          int      in, j, i = 0;
 // statement in main().
                                            cout << "L(1) M(2) C(3) Go(0): ";
 #include <iostream.h>                      cin >> in;
                                            while (in) {
 class Stooge { public:                        roles[i++] = Factory::create(in);
    virtual void slapStick() = 0;              cout << "L(1) M(2) C(3) Go(0): ";
 };                                            cin >> in; }

 class Larry : public Stooge { public:      for (j=0; j < i; j++)
    void slapStick() {                         roles[j]->slapStick();
       cout << "Larry: poke eyes"
          << endl; }                        for (j=0; j < i; j++)
 };                                            delete roles[j];
 class Moe : public Stooge { public:     }
    void slapStick() {
       cout << "Moe: slap head"          class Larry : public Stooge { public:
          << endl; }                        Stooge* clone() { return new Larry;
 };                                         void slapStick() {
 class Curly : public Stooge { public:         cout << "Larry: poke eyes"
    void slapStick() {                            << endl; }
       cout << "Curly: suffer abuse"     };
          << endl; }                     class Moe : public Stooge { public:
 };                                         Stooge* clone() { return new Moe; }
                                            void slapStick() {
 void main( void )                             cout << "Moe: slap head"
 {                                                << endl; }
    Stooge*  roles[10];                  };
    int      in, j, i = 0;               class Curly : public Stooge { public:
                                            Stooge* clone() {return new Curly;}
    cout << "L(1) M(2) C(3) Go(0): ";       void slapStick() {
    cin >> in;                                 cout << "Curly: suffer abuse"
    while (in) {                                  << endl; }
       if (in == 1)                      };
          roles[i++] = new Larry;
       else if (in == 2)                 Stooge* Factory::prototypes_[] = { 0,
          roles[i++] = new Moe;             new Larry, new Moe, new Curly };
       else                              Stooge* Factory::create( int i ) {
          roles[i++] = new Curly;           return prototypes_[i]->clone(); }
       cout << "L(1) M(2) C(3) Go(0): ";
       cin >> in; }                      // L(1) M(2) C(3) Go(0): 1
                                         // L(1) M(2) C(3) Go(0): 2
    for (j=0; j < i; j++)                // L(1) M(2) C(3) Go(0): 3
       roles[j]->slapStick();            // 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
       delete roles[j];                  // Moe: slap head
 }                                       // Curly: suffer abuse
                                         // Larry: poke eyes

