Query - I would like to know how I should proceed to code if I need to make the information in the Observer class available to methods in other classes.
The observer receives the updates from Observable in the form of Object[]. I am able to print the output onto console, but not clear on how to make this information available to other classes.
I tried by creating a getInfo method in the Observer which returns the Object[] & then instantiating the Observer in the class where I need the info, calling the getInfo method. But I am getting a null pointer exception.
Code - Observer
private String car;
private CarModel carModel;
public broadcastObserver(Observable observable){
observable.addObserver(this);
}
@Override
public void update(Observable observable, Object arg) {
Object[] subjectMessage = (Object[])arg;
int i = 0;
for (Object o : subjectMessage) {
switch (i){
case 0:
car = (String)o;
i++;
break;
case 1:
carModel = (CarModel) o;
i++;
break;
}
}
}
public Object[] getObserverFeed(){
Object[] observerFeed = new Object[]{car,carModel};
return observerFeed;
}
Code - Class Method where info is needed
broadcastObserver bO = new broadcastObserver();
Object[] feed = bO.getObserverFeed();
Invert the flow of data: instead of trying to pull info from the observer with get-methods, use the observer to push it to where it's needed.