java.util.Observer Example
In this example, we shall be discussing about the java.util.Observer interface
. The Observer
interface is used to notify “Observers” of the changes made to the Obervable
. It is mainly used to implement the Observer Pattern in Java. It has been around since Java 1.
The Observer
interface has only one method, void update(Observable o, Object arg)
. The Observable o
is the observable object from java.util.Observable
class. The arg
object is any optional object that can be passed as argument by the notifying Observable
class. A class that implements the Observer interface, declares itself eligible to listen to the changes made to the observables.
1. Observable
The java.util.Observable
class is used along with Observer
instance to implement the Observer pattern. A class whose changes are to be tracked by the observers, extends the java.util.Observable
class. This class has implemented methods for updating/notifying the Observers about the changes made to the Observable. It also provides method to the Observer
instances to hook on with itself, or unhook. Few methods in the java.util.Observable
class are:
public void addObserver(Observer o)
Add an Observer.public void deleteObserver(Observer o)
Delete an Observer.public void notifyObservers()
notify observers of changes.
2. Observer Pattern
The Observer
and Observable
are used to implement the Observer Pattern in Java. This pattern is used when a multiple number of instances called Observers are listening to changes to a particular class called Observable. For example, if the underlying data-source changes, all the views using that data-source should reflect the changes.
Here is a simple weather reporting system, which tries to implement the Observer Pattern using the Observer
interface and Observable
class,
ObservableDemo.java:
package com.javacodegeeks.examples; import java.util.Observable; public class ObservableDemo extends Observable { private String weather; public ObservableDemo(String weather) { this.weather = weather; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; setChanged(); notifyObservers(); } }
ObserverExample.java:
package com.javacodegeeks.examples; import java.util.Observable; import java.util.Observer; public class ObserverExample implements Observer { private ObservableDemo weatherUpdate ; @Override public void update(Observable observable, Object arg) { weatherUpdate = (ObservableDemo) observable; System.out.println("Weather Report Live. Its "+weatherUpdate.getWeather()); } public static void main(String[] args) { ObservableDemo observable = new ObservableDemo(null); ObserverExample observer = new ObserverExample(); observable.addObserver(observer); observable.setWeather("Bright and sunny...Let's play cricket!! "); observable.setWeather("Raining Heavily!..Let's have hot Pakodas!!"); } }
OUTPUT:
Weather Report Live. Its Bright and sunny...Let's play cricket!! Weather Report Live. Its Raining Heavily!..Let's have hot Pakodas!!
The Observer
and Observable
instances are created and then the observer is hooked with the Observable. Once attached to the Observable, it can notify the changes made to itself to the Observer. On notification the Observer executes its update
method.
Conclusion
Here, we tried to understand the basics of Observer
interface and how we can use the same for implementing the Observer Pattern in Java.
You can download the source code of this example here: ObserverExample.zip