Code example for Observable

Methods: addObserver

0
    private float humidity;
    private float temperature;
 
    public CurrentConditionsDisplay(Observable observable){
        this.observable = observable;
        observable.addObserver(this);
    } 
 
    public void update(Observable observable,Object arg){
        if (observable instanceof WeatherData){
            WeatherData weatherData = (WeatherData)observable;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display(); 
        } 
    } 
 
    public void display(){ 
        System.out.println("Current conditions: " + temperature);
    } 
}