Code example for Observable

Methods: addObserver

0
package com.sayem.observer.weatherobservable; 
 
import java.util.Observable; 
import java.util.Observer; 
 
public class ForecastDisplay implements Observer, DisplayElement {
	private float currentPressure = 29.92f;  
	private float lastPressure;
 
	public ForecastDisplay(Observable observable) {
		observable.addObserver(this);
	} 
 
	public void update(Observable observable, Object arg) {
		if (observable instanceof WeatherData) {
			WeatherData weatherData = (WeatherData)observable;
			lastPressure = currentPressure;
			currentPressure = weatherData.getPressure();
			display(); 
		} 
	} 
 
	public void display() { 
		System.out.print("Forecast: ");
		if (currentPressure > lastPressure) {
			System.out.println("Improving weather on the way!");