282
Chapter 8
CHAPTER 8
Observer Pattern 8
It is the theory that decides what can be observed.
—Albert Einstein
Every man who observes vigilantly and resolves
steadfastly grows unconsciously into genius.
—Edward G. Bulwer-Lytton (Author of the immortal
line, “It was a dark and stormy night,” written while
trying to explain how his Internet connection got
knocked out and he missed a deadline.)
The world is full of obvious things which nobody by
any chance ever observes.
—Sherlock Holmes (Original reference to syntax errors.)
You can observe a lot just by watching.
—Yogi Berra
What Is the Observer Pattern?
Conceptually, the Observer design pattern is easy to understand. A central point
sends information to subscribing instances. This works just like a newspaper or cable
television subscription service. When a person subscribes, the service begins, and
continues until he unsubscribes.
In applications where a single source of information needs to be broadcast to several
different receptors, using a single source in the design makes more sense than having
several different sources getting the same information by repeated calls to the data
source. For example, in using a web service that sends out stock quotes, setting up
your application to receive the information in a single source, and then sending out
that information from that source in your application, is more efficient than having
each instance calling the information separately from the web service. If your appli-
cation takes the incoming stock information and displays the information in tabular
and different chart forms, having multiple subscriptions to each of the different
formatting classes would require separate and repeated calls to the web service.