Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
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.
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 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.