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.

Figure 17-2 implementing Customer with Obs erver.<br />

Chapter 17 • The Observer Pattern 269<br />

2. When a new Customer is added, the notify method calls<br />

these Observers.<br />

Each observer calls getstate for information on the newly<br />

added Customer to see what it needs to do. Note: Typically, there<br />

would be several methods to get the needed information.<br />

Note in this case, we use static methods for attach and detach<br />

because observers want to be notified for all new Customers. When<br />

notified, they are passed the reference to the Customer created.<br />

Example 17-1 shows some of the code required to implement this.<br />

This approach allows me to add new Observers without affecting Observer aids<br />

any existing classes. It also keeps everything loosely coupled. This flexibility and keeps<br />

organization works if I have kept all of the objects responsible for things decoupled<br />

themselves.<br />

How well does this work if I get a new requirement? For example, New requirement:<br />

what if I need to send a letter with coupons to customers located send coupons, too<br />

within 20 miles of one of the company's "brick and mortar" stores.

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

Saved successfully!

Ooh no, something went wrong!