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

An inside view of Observer

The Observer pattern facilitates communication between decoupled objects

  • Print
  • Feedback

Page 2 of 6

Example 1 specifically shows use of the Observer pattern with Swing. Before we discuss more Observer pattern details, let's look at how the pattern is generally implemented.

How the Observer pattern works

Figure 2 shows how objects in the Observer pattern are related.

Figure 2. Observer pattern class diagram

The subject, which is an event source, maintains a collection of observers and provides methods to add and remove observers from that collection. The subject also implements a notify() method that notifies each registered observer about events that interest the observer. Subjects notify observers by invoking the observer's update() method.

Figure 3 shows a sequence diagram for the Observer pattern.

Figure 3. Observer pattern interaction. Click on thumbnail to view full-size image.

Typically, some unrelated object will invoke a subject's method that modifies the subject's state. When that happens, the subject invokes its own notify() method, which iterates over the collection of observers, calling each observer's update() method.

The Observer pattern is one of the most fundamental design patterns because it allows highly decoupled objects to communicate. In Example 1, the only thing the bounded range model knows about its listeners is that they implement a stateChanged() method. The listeners are only interested in the model's value, not how the model is implemented. The model and its listeners know very little about one another, but thanks to the Observer pattern, they can communicate. That high degree of decoupling between models and listeners lets you build software composed of pluggable objects, making your code highly flexible and reusable.

The Java 2 SDK and the Observer pattern

The Java 2 SDK provides a classic implementation of the Observer pattern with the Observer interface and the Observable class from the java.util directory. The Observable class represents the subject; observers implement the Observer interface. Interestingly, this classic Observer pattern implementation is rarely used in practice because it requires subjects to extend the Observable class. Requiring inheritance in this case is a poor design because potentially any type of object is a subject candidate, and because Java doesn't support multiple inheritance; often, those subject candidates already have a superclass.

The event-based implementation of the Observer pattern, which was used in the preceding example, is the overwhelming choice for Observer pattern implementation because it doesn't require subjects to extend a particular class. Instead, subjects follow a convention that requires the following public listener registration methods:

  • void addXXXListener(XXXListener)
  • void removeXXXListener(XXXListener)

Whenever a subject's bound property (a property that's been observed by listeners) changes, the subject iterates over its listeners and invokes the method defined by the XXXListener interface.

By now you should have a good grasp of the Observer pattern. The rest of this article focuses on some of the Observer pattern's finer points.

  • Print
  • Feedback

Resources