23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the observers must supply the Subject with the appropriate<br />

Strategy object to use.<br />

Summary<br />

Chapter 17 • The Observer Pattern 275<br />

In learning the Observer pattern, I looked at which object is best<br />

able to handle future variation. In the case of the Observer pattern,<br />

the object that is triggering the event— the Subject— cannot anticipate<br />

every object that might need to know about the event. To solve<br />

this, I create an Observer interface and require that all Observers be<br />

responsible for registering themselves with this Subject.<br />

While I focused on the Observer pattern during the chapter, it is<br />

worth pointing out several object-oriented principles that are used<br />

in the Observer pattern.<br />

Concept Discussion<br />

Objects are There were different kinds of Observers but all gath-<br />

responsible for ered the information they needed from the Subj ect<br />

themselves and took the action appropriate for them on their own.<br />

Abstract class The Observer class represents the concept of<br />

objects that needed to be notified. It gave a common<br />

interface for the subject to notify the Observers.<br />

Polymorphic The subject did not know what kind of observer it was<br />

encapsulation communicating with. Essentially, the Observer class<br />

encapsulated the particular Observers present. This<br />

means that if I get new Observers in the future, the<br />

Subject does not need to change.<br />

In this chapter<br />

Summary of<br />

object-oriented<br />

principles used

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

Saved successfully!

Ooh no, something went wrong!