23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

276 Part V • Handling Variations with <strong>Design</strong> <strong>Patterns</strong><br />

Supplement: C++ Code Example<br />

Example 17-2 C++ Code Fragment<br />

class Customer {<br />

public:<br />

static void attach(Observer *o);<br />

static void detach(Observer *o);<br />

String getStateO;<br />

private:<br />

Vector myObs;<br />

void notifyObs();<br />

}<br />

Customer::attach(Observer *o){<br />

myObs.addElement(o); }<br />

Customer::detach(Observer *o){<br />

myObs.remove(o);<br />

}<br />

Customer::getState () {<br />

// have other methods that will //<br />

give the required information<br />

}<br />

Customer::notifyObs () { for<br />

(Enumeration e =<br />

myObs.elements();<br />

e.hasMoreElements() ;)<br />

{ ((Observer *) e)-><br />

update(this); }<br />

><br />

}<br />

class Observer { public: Observer();<br />

void update(Customer *mycust)=0;<br />

// makes this abstract }<br />

(continued)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!