一个可观察的多个可观察事件
我试图解决在解释一个可观察对象的 update() 调用时所涉及的丑陋的条件转换,该可观察对象想要通知其观察者多种类型的事件。另外,我不想将标志传递给 notifyObservers()/update() 方法。
我不希望观察者必须轮询可观察对象来找出发生了什么变化,我希望通过 update() 方法(或类似方法)将这些新数据提供给观察者。
我有一个解决方案的想法。我为每种类型的通知实例化一个可观察对象。例如: observable 是一个代表硬件设备的对象,它将包含代表其状态的 observables:
public final Observable connectionState = new Observable();
public final Observable dataState = new Observable ();
这样,观察者不需要做任何类型的查询或条件转换,作为一个观察者,即一个重写的 update() 方法可以按通知类型使用。
经过多次挫折后,这是我能想到的最优雅的解决方案,但是我有一种可怕的感觉,我错过了如何正确使用观察者/可观察对象的要点。
我对该解决方案的主要问题是:
- 它仍然涉及强制转换(至少它不是有条件的)
- 由于可观察量需要可观察,因此它们必须是公共成员。虽然这确实允许观察者调用 addObservable(),但也允许他们调用 notifyObservers()。
我做的事正确吗?
谢谢
I'm trying to get around the ugly conditional casting involved when interpreting an update() call for an observable that will want to notify its observers of multiple types of events. Also, I'd prefer not to pass flags to the notifyObservers()/update() method.
I do not want the observers to have to poll the observable object to find out what's changed, I'd like this new data to be given to the observers via the update() method (or similar.)
I have an idea for a solution. I instanciate one observable object for each type of notification. For example: The observable is an object representing a hardware device, it will contain observables representing its state:
public final Observable connectionState = new Observable();
public final Observable dataState = new Observable ();
This way, observers don't need to do any sort of querying or conditional casting, as one observer, i.e. one overridden update() method can be used per notification type.
After much frustration this is the most elegant solution I can think of, however I have this horrible feeling that I've missed the point about how to use Observers/Observables properly.
My main issues with that solution are:
- It still involves a cast (at least it's not conditional)
- Since the observables need to be observable, they must be public members. While this does allow observers to call addObservable(), it also allows them to call notifyObservers().
Am I doing the right thing?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在努力应对观察者模式的 Java 1.0 实现的限制。看看这个问题的这个答案 java.util.Observable 是否在任何地方使用?
也许您应该实现您自己的更适合您需求的模式版本,而不是尝试将您的需求硬塞到 java.util.Observer 中。
You are struggling with the limitations of the Java 1.0 implementation of the Observer pattern. Take a look at this answer to the question Is java.util.Observable used anywhere?
Rather than trying to shoehorn your requirements into java.util.Observer, maybe you should just implement your own version of the pattern that better fits your needs.
您可以尝试将 Observable 与 Visitor 模式配对使用:
You can try to use Observable paired together with Visitor pattern:
您的问题有很多可能的解决方案。如果您觉得 Observable 类不适合您的问题,您可以维护自己的侦听器集合。
There are lots of possible solutions for your problem. If you feel the Observable class isn't suited to your problem you can maintain your own collection of listeners.