Home > Articles > Programming > Java

Design Patterns in Java: The Observer

  • Print
  • + Share This
Need to implement a publish-and-subscribe mechanism? The observer pattern is simple and flexible and it provides good separation of concerns. The observer pattern is also a key element of Java. Read this article by Stephen Morris for details.
Like this article? We recommend Teaser

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.

Data Sharing

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.

  • + Share This
  • 🔖 Save To Your Account