mercredi 15 juillet 2020

How the observer is added in the constructor?

public static void main(String[] args) {
        WeatherData subject =new WeatherData();
        new ShowEstadistics(subject);
} 


public class WeatherData implements Subject {

    ArrayList<Observer> observers;

    public WeatherData() {
        observers = new ArrayList();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }
}



public class ShowEstadistics implements Observer{

WeatherData weatherdata;

public ShowEstadistics(WeatherData subject) {
     this.weatherdata=subject;

     this.weatherdata.registerObserver(this);
        
    }

I dont know how the observer is registered in subject in this part:

public ShowEstadistics(WeatherData subject) {
         this.weatherdata=subject;
    
         this.weatherdata.registerObserver(this);
            
        } 

because I apply the registerObserver method to weatherdata but for some reason it is registered in subject too.I dont know why.

Someone explain me please.

Aucun commentaire:

Enregistrer un commentaire