Supplement: C++ Code Example

Example 17-2. C++ Code Fragment
class Customer {

 public:
  static void attach(Observer *o);
  static void detach(Observer *o);
  String getState();
 private:
  Vector myObs;
  void notifyObs();
}

Customer::attach(Observer *o){
  myObs.addElement(o);
}
Customer::detach(Observer *o){
  myObs.remove(o);
}
Customer::getState () {
    // have other methods that will
    // give the required information
}

Customer::notifyObs () {
    for (Enumeration e =
      myObs.elements();
      e.hasMoreElements() ;) {
       ((Observer *) e)->
         update(this);
    }
  }
}

class Observer {
 public:
  Observer();
  void update(Customer *mycust)=0;
    // makes this abstract
}
Observer::Observer () {
    Customer.attach( this);
}
class AddrVerification : public Observer {
 public:
  AddrVerification();
  void update( Customer *myCust);
}

AddrVerification::AddrVerification () {
}
AddrVerification::update
  (Customer *myCust) {
    // do Address verification stuff here
    // can get more information about
    // customer in question by using myCust
}

class WelcomeLetter : public Observer {
 public:
  WelcomeLetter();
  void update( Customer *myCust);
}

WelcomeLetter::update( Customer *myCust) {
    // do Welcome Letter stuff here can get more
    // information about customer in question by
    // using myCust
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.157.182