多个观察者和多个可观察者
我使用JAVA中的Observable类/观察者接口来实现观察者模式。如果我有一个对象,我希望能够观察其他几个可观察对象(多个可观察对象),并且有多个观察者(多个观察者),
那么问题不是 B 类中的 Observable ,而是我想在 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)}
有什么建议吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过让传递的对象成为对象的
Collection
,可以实现多个可观察对象。您还可以轻松拥有多个观察者或可观察对象。考虑以下示例:
可以使用以下方式将其连接在一起:
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:
Which can be wired together using:
应该可以正常工作。只需将该单个视图/观察者添加到您想要观察的所有模型/可观察对象中即可。
should work fine. just add that singe view/observer to all of the models/observables that you want to observe.