替代 Java 的 Observable 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10218895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative to Java's Observable class?
提问by HolySamosa
I'm coming to Java from the C# world where the Observer pattern is implemented as a first-class language construct with the event
keyword.
我是从 C# 世界来到 Java 的,在那里观察者模式被实现为带有event
关键字的一流语言构造。
I see that Java's had the Observable
class since the early days, but it clearly has implementation issues and doesn't seem to be widely used. Thus far I've just been rolling-my-own implementation of the Observer pattern in my Java code but I always can't help but think there must be a better alternative to always turning out this boilerplate code. There are the Listener classes in Swing but they don't seem appropriate for non-Swing code.
我看到 JavaObservable
从早期就有这个类,但它显然存在实现问题并且似乎没有被广泛使用。到目前为止,我只是在我的 Java 代码中滚动我自己的 Observer 模式实现,但我总是忍不住认为必须有更好的替代方案来始终生成这个样板代码。Swing 中有 Listener 类,但它们似乎不适用于非 Swing 代码。
What's the recommended solution for this oh-so-common problem? Third-party libraries are ok with me.
对于这个非常普遍的问题,推荐的解决方案是什么?第三方库对我没问题。
采纳答案by Joni
Usually the observer pattern is implemented with an ad-hoc solution when ever it's needed. When it comes to behavioral patterns it's one of the simplest: two methods to control a list of "observers," and one method that notifies the observers when something interesting happens.
通常,观察者模式是在需要时使用临时解决方案实现的。当谈到行为模式时,它是最简单的模式之一:控制“观察者”列表的两种方法,以及一种在有趣的事情发生时通知观察者的方法。
The observer pattern tends to arise too infrequently outside of certain domains, and when it does arise it tends be too specific to the application domain, so generic solutions are of little value. You can see the problem with the java.util.Observable
class: if you inherit from Observable
you have to accept every possible kind of "observer" that may be passed to you, which likely makes no sense for your application:
观察者模式往往在某些领域之外很少出现,当它出现时,它往往过于特定于应用程序领域,因此通用解决方案几乎没有价值。你可以看到这个java.util.Observable
类的问题:如果你继承自Observable
你必须接受可能传递给你的每一种可能的“观察者”,这对你的应用程序可能没有意义:
- What if your object can produce two different kinds of events, and thus needs two different lists of observers?
- What if the observers may influence your object depending on the event, e.g. by being able to veto a change?
- 如果您的对象可以产生两种不同类型的事件,因此需要两个不同的观察者列表怎么办?
- 如果观察者可能会根据事件影响您的对象,例如能够否决更改,该怎么办?
That said, EventBus from Google Guavaseems to do a pretty good job of simplifying event producing and handling by taking a radically different approach.
也就是说,来自 Google Guava 的 EventBus似乎通过采用完全不同的方法在简化事件生成和处理方面做得很好。
Other techniques based on annotations have been discussed on SObefore.
其他基于注释的技术之前已经在 SO 上讨论过。
回答by thSoft
I recommend looking into reactive programming. Java has only one implementation that I know of: reactive4java.
我建议研究响应式编程。Java 只有一种我知道的实现:reactive4java。
回答by Carlos Gavidia-Calderon
This articleshows a nice implementation of the pattern using Spring Framework. You still need to define your Subject and Observer interfaces (and implementations), but Spring handles the Observer registration to Subjects through XML configuration.
本文展示了使用 Spring Framework 的模式的一个很好的实现。你仍然需要定义你的 Subject 和 Observer 接口(和实现),但是 Spring 通过 XML 配置处理 Observer 注册到 Subjects。