Design Patterns in Java: The Observer
One organization I worked for had an interesting approach to making corporate announcements: using voicemail. The message was recorded and posted, and then anybody in the company could listen to it by phone.
I kind of liked this approach because it allowed for an element of the personal touch and was a little less formal than a long rambling email message.
This mechanism is similar to the "observer or the publish-and-subscribe pattern." In this case, the voicemail message was recorded and posted (that is, published) and interested parties could then listen (or subscribe) to the message.
The publish-and-subscribe pattern is commonly known as the observer pattern, and a Java implementation of it is the topic of this article. One of the best aspects of the observer pattern is the economy it affords—the data is stored just once, and the observers can easily keep track of changes. As you’ll see, a key part of this is that the observers are asynchronously updated.
Let’s have a look at an example of the observer pattern.