简体   繁体   English

多观察者和多观察者

[英]Multiple Observer and Multiple Observable

I'm using the Observable class/Observer interface in JAVA to implement the observer pattern. 我正在使用JAVA中的Observable类/观察器接口来实现观察者模式。 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 问题在类B中不是anobservable的,但我想在A和B中赋值以生成图表

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. 谢谢。

Multiple observable objects can be achieved by letting the passed object be a Collection of objects. 通过将传递的对象作为对象的Collection ,可以实现多个可观察对象。

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());

should work fine. 应该工作正常。 just add that singe view/observer to all of the models/observables that you want to observe. 只需将该单一视图/观察者添加到您要观察的所有模型/可观察对象中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM