Skip to content
Advertisement

Java built-in Observable push notifications

UPDATE Added the full code to make it easier to understand.

I am trying to understand how to implement push vs pull notifications using Java built-in Observer.

the Observable class has 2 methods notifyObservers() and notifyObservers(Object arg)

according to the docs: Each observer has its update method called with two arguments: this observable object and the arg argument.

here is my Observer class

JavaScript

and in my observable class

JavaScript

I’ve tried both methods, and both objects can be cast down to WeatherData(the Observer) and then get the data from them.

using both methods seem like push notification-type for me, so what is the difference? and how can I implement pull notification-type using it?

Advertisement

Answer

From the documentation of notifyObservers():

Each observer has its update method called with two arguments: this observable object and null. In other words, this method is equivalent to: notifyObservers(null)

That means that you should never call notifyObservers(this)—it’s redundant. Normally, the argument would be additional data about the change event. This is analogous to more modern event listener classes (Observable and Observer are now deprecated) whose events contain data in addition to the event source. For example, if you add an ActionListener to a button, pressing the button at runtime causes the ActionListener’s actionPerformed method to be called with an event that contains data such as the time when the action occurred.

A pull notification isn’t really a notification at all. Pulling means you don’t wait to be told that something changed; you ask whether anything has happened.

An example of this would be keeping a boolean field in your class that indicates whether any changes have occurred:

JavaScript

As for how to do the pulling, just keep a reference to a WeatherData object in your CurrentConditionsDisplay class, and check whether it has changed:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement