mercredi 11 mars 2020

Java Provided Observable not a good practice

public void update(Observable obs, Object arg) 
{
  if (obs instanceof WeatherData) {
  WeatherData weatherData = (WeatherData)obs;
  this.temperature = weatherData.getTemperature();
  this.humidity = weatherData.getHumidity();
  display();
  }
 }

The above piece of code is from Java Head First Pattern, now the observable is going to throw for every change that is done and this will reach to all the observers even though the object is what they expect or not and if block helps in letting the code know. So create observable for every such scenario will be a good practice right? Please answer

Aucun commentaire:

Enregistrer un commentaire