Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Speaking on the Observer pattern

How can you use the Observer pattern in your Java design?

  • Print
  • Feedback

Q I want to use the Java Observer pattern in my project. With that in mind, can you give me some sample code to demonstrate how it works?

A Just as object-oriented programming encourages code reuse, design patterns encourage design reuse. Indeed, design patterns allow you to reuse clean, time-tested designs. However, design patterns have come under an increasing amount of criticism lately. Critics point out that inexperienced developers can easily fall into the pattern trap.

The pattern trap blinds inexperienced developers. As such, instead of solving a solution in the best way possible, the end goal for novice developers centers on implementation of as many design patterns as possible. To some, using a design pattern seems to guarantee a good design. By that logic, if you use a lot of design patterns, you'll have a great design! Often times this belief leads to designs that just don't make sense -- even though the design might incorporate multiple patterns. Unfortunately, a design pattern does not guarantee good design.

In order to properly use a design pattern in your design, you must uphold three criteria:

  1. Understand your problem
  2. Understand the pattern
  3. Understand how the pattern solves your problem


You need to know criterion number 1 first and foremost. How can you apply a pattern if you do not fully know what problem you are trying to solve?

You also need to know the second criterion: You must fully understand the patterns you are interested in applying. How can you apply a pattern if you do not understand it? More importantly, how can you even consider a pattern if you do not understand what it does?

Finally, if you cannot articulate how the pattern will solve your problem (why it is appropriate), forget it. Don't fall into the pattern trap and simply use a pattern so you can say you used it.

I'm not saying that the reader is necessarily falling into this trap. However, by the wording of the question, many developers may get the wrong impression about patterns. From the question, I understand that the reader probably knows the problem and understands the Observer pattern. He or she simply needs to see the pattern implemented in Java.

Before giving a Java example, it may help other readers to first give a brief description of the Observer pattern.

Simply, the Observer pattern allows one object (the observer) to watch another (the subject). The Observer pattern allows the subject and observer to form a publish-subscribe relationship. Through the Observer pattern, observers can register to receive events from the subject. When the subject needs to inform its observers of an event, it simply sends the event to each observer.

For example, you might have a spreadsheet that has an underlying data model. Whenever the data model changes, the spreadsheet will need to update the spreadsheet screen and an embedded graph. In this example, the subject is the data model and the observers are the screen and graph. When the observers receive notification that the model has changes, they can update themselves.

  • Print
  • Feedback

Resources