0
votes

I'm using the Observable class/Observer interface in JAVA to implement the observer pattern. If I have an object that I want to be able to observe several other observable object (multiple observables) and have several observer (multiple observer)

The problem is not anObservable in class B , But I want to value in A and B for generate chart

public void update(Observable anObservable, Object anObject) {
    if(anObservable instanceof A){
        createDataSet(anObservable,null);
    }
    else if(anObservable instanceof B)
    {
        createDataSet(null,anObservable);
    }       
}
private  void (Observable anSampleObservable,Observable anAreaObservable){
// To do something with value in anSampleObservable (A) and value in anAreaObservable(B)}

Any advice? Thanks.

2

2 Answers

0
votes

Multiple observable objects can be achieved by letting the passed object be a Collection of objects.

You can also easily have multiple observers or observables. Consider the following sample:

class Model1 extends Observable {}
class Model2 extends Observable {}

class Controller1 implements Observer {
    public void update(Observable o, Object object) {}
}    

class Controller2 implements Observer {
    public void update(Observable o, Object object) {}
}

Which can be wired together using:

Model1 model1 = new Model1();
Model2 model2 = new Model2();
model1.addObserver(new Controller1());
model2.addObserver(new Controller2());
0
votes

should work fine. just add that singe view/observer to all of the models/observables that you want to observe.