 // Purpose.  Command                    class Deadbeat {
 //                                      public:
 // Discussion.  On the left, an IOU        Deadbeat( int v ) { cash_ = v; }
 // has been encapsulated as a struct,      int payUp( int v ) {
 // and TheBoss class is tightly coup-         cash_ -= v;  return v; }
 // led to that struct.  On the right,      int rptCash() { return cash_; }
 // TheBoss is only coupled to the ab-   private:
 // stract class Command.  Lots of pos-     int cash_;
 // sible derived classes could be sub-  };
 // stituted: IOUs that call payUp() on
 // Deadbeats, Checks that call cash()   class Command { public:
 // on Banks, Stocks that call redeem()     virtual int execute() = 0;
 // on Companies.  Each "command" is a   };
 // "token" that gets transfered from
 // one holder to another, until some-   class IOU : public Command {
 // one chooses to "execute" it.         public:
                                            typedef int (Deadbeat::*Meth)(int);
 class Deadbeat { public:                   IOU( Deadbeat* r, Meth a, int m ) {
    Deadbeat( int v ) { cash_ = v; }           obj_ = r;
    int payUp( int v ) {                       mth_ = a;
       cash_ -= v;  return v; }                amt_ = m; }
    int rptCash() { return cash_; }         int execute() {
 private:                                      return (obj_->*mth_)( amt_ ); }
    int cash_;                           private:
 };                                         Deadbeat*  obj_;
                                            Meth       mth_;
 struct IOU {                               int        amt_;
    Deadbeat*  objPtr;                   };
    int (Deadbeat::*funPtr)( int );
    int        amt;                      class Enforcer {
 };                                      public:
                                            Enforcer( Command& c ) : cmd_(c) {}
 class Enforcer { public:                   Command& collect() { return cmd_; }
    Enforcer( IOU& m ) : mkr_(m) { }     private:
    IOU& collect() { return mkr_; }         Command& cmd_;
 private:                                };
    IOU& mkr_;
 };                                      class TheBoss {
                                         public:
 class TheBoss {                            TheBoss() { cash_ = 1000; }
 public:                                    collect( Command& cmd ) {
    TheBoss() { cash_ = 1000; }                cash_ += cmd.execute(); }
    collect( IOU& i ) {                     int rptCash() { return cash_; }
       cash_ +=                          private:
          ((i.objPtr)->*i.funPtr)(i.amt)    int cash_;
    }                                    };
    int rptCash() { return cash_; }
 private:                                void main( void )
    int cash_;                           {
 };                                         Deadbeat joe(90), tom(90);
                                            IOU one(&joe, &Deadbeat::payUp, 60);
 void main( void )                          IOU two(&tom, &Deadbeat::payUp, 70);
 {                                          Enforcer quido(one), lucca(two);
    Deadbeat joe(90), tom(90);              TheBoss  don;
    IOU one ={&joe, &Deadbeat::payUp,60}
    IOU two ={&tom, &Deadbeat::payUp,70}    don.collect( quido.collect() );
    Enforcer quido(one), lucca(two);        don.collect( lucca.collect() );
    TheBoss  don;                           cout << "joe has $" << joe.rptCash()
                                            cout << "tom has $" << tom.rptCash()
    don.collect( quido.collect() );         cout << "don has $" << don.rptCash()
    don.collect( lucca.collect() );      }
    cout << "joe has $" << joe.rptCash()
    cout << "tom has $" << tom.rptCash() // joe has $30
    cout << "don has $" << don.rptCash() // tom has $20
 }                                       // don has $1130

