Use of Observers in the Java API
The Observer design pattern uses a push model. In the push model, observers don't make requests for new information. Instead, the subject pushes new, unsolicited information to all registered observers. The subject pushes by calling each observer's update() method.
The opposite of the push model is a pull model. In the pull model, observers make requests for new information. (Observers pull information from the subject.) Each observer pulls information by calling the subject's getter methods.
The Java API has a built-in java.util.Observable class and a java.util.Observer interface to support both push and pull models.
The Observable class has pre-defined methods to manage observers:
void addObserver(Observer observer);
void deleteObserver(Observer observer);
void notifyObservers();
void notifyObservers(Object args);
The Observable class also has methods to keep track of state changes (e.g., changes in stock quote values):
void setChanged();
boolean hasChanged();
void clearChanged();
To demonstrate the pull model, the stock quote application has been modified to use the Java API versions of Observable and Observer. Listings 7 and 8 contain the modified code.
In Listing 7, the java.util.Observable class takes the place of the Subject interface (from Listing 1). The Observable class has pre-defined methods to manage observers, so the new StockData class in Listing 7 doesn't need to maintain its own ArrayList.
The new BigBuyer class (Listing 8) stores any java.util.Observable instance. Before calling methods like getSymbol() and getVolume(), the BigBuyer class's update() method must do some casting.
The essential difference between the pull and push models is the flow of control. In the push model (Listings 1 to 6), the flow goes like this:
- Someone calls the
stockData object's setStockData() method.
- Within
stockData, the setStockData() method (directly or indirectly) calls the notifyObservers() method.
- The notifyObservers() method reaches out of the stockData object to call the bigBuyer object's update() method.
With the push model, most of the action takes place in the StockData class. But in the pull model (Listings 7 and 8), the flow works differently:
With the push model, most of the action takes place in the StockData class. But in the pull model (Listings 7 and 8), the flow works differently:
- Someone calls the
stockData object's setStockData() method.
- Within
stockData, the setStockData() method (directly or indirectly) calls the setChanged() and notifyObservers() methods.
- Behind the scenes, in the Java API, the call to
notifyObservers() triggers a call to the bigBuyer's update() method. Now the ball is in bigBuyer's court!
- The bigBuyer object takes the initiative to reach out and pull in the new stock quote data. That is, the
bigBuyer object calls the stockData's getter methods. Herein lies the difference between pushing and pulling. With the pull model, the bigBuyer observer pulls information from the stockData observable.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|