java.util.Observable 是否在任何地方使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2380676/
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
Is java.util.Observable used anywhere?
提问by MHarris
Was chatting to a colleague about the design of Javas libraries. I mentioned I'd always found it funny that the AWT/Swing classes called that essentially provide an Observable interface on a UI widget were actually called "listeners".
正在和同事聊 Java 库的设计。我提到我总是觉得有趣的是,所谓的 AWT/Swing 类实际上在 UI 小部件上提供了一个 Observable 接口,但实际上被称为“侦听器”。
Whereupon he floored me by pointing out that there would have been a name clash with java.util.Observerand java.util.Observable, which have been around since JDK 1.0. I'd never heard of them before.
因此,他指出自 JDK 1.0 以来就存在与java.util.Observer和java.util.Observable的名称冲突,这让我感到震惊。我以前从未听说过他们。
There are no usages listed in the JavaDocs and google/googlecodedon't come up with much, so does anyone know if they are used somewhere in the JDK? They don't seem to have received any Generic love, so presumably they're not "popular". Has anyone found them useful?
JavaDocs 中没有列出用法,google/ googlecode也没有提出太多,所以有人知道它们是否在 JDK 中的某个地方使用过吗?他们似乎没有收到任何通用的爱,所以想必他们并不“受欢迎”。有没有人发现它们有用?
采纳答案by Chris Dennett
Observerand Observableare still used in a lot of Eclipse wizard code for detecting when the stuff in input boxes changes, so the wizard itself can be updated. I've had to work with some of this stuff, changing it for our own needs.. :)
Observer并且Observable仍然在很多 Eclipse 向导代码中用于检测输入框中的内容何时发生变化,因此向导本身可以更新。我不得不使用其中的一些东西,根据我们自己的需要对其进行更改.. :)
回答by Péter T?r?k
They are not used, because their design is flawed: they are not type safe. You can attach any object that implements Observer to any Observable, which can result in subtle bugs down the line.
它们没有被使用,因为它们的设计有缺陷:它们不是类型安全的。您可以将任何实现 Observer 的对象附加到任何 Observable,这可能会导致细微的错误。
Wrapping them inside a type safe interface is about the same amount of work as implementing the pattern from scratch, so I guess the latter is preferred in most cases.
将它们包装在类型安全的接口中与从头开始实现模式的工作量大致相同,所以我猜在大多数情况下后者是首选。
This is one of the things which are flawed in Java 1.0 due to suboptimal design choices made under time pressure (others include the Java 1.0 Collection API and java.util.Date), but due to the nature of public APIs can never anymore be removed (only deprecated).
这是 Java 1.0 中的缺陷之一,因为在时间压力下做出了次优的设计选择(其他包括 Java 1.0 Collection API 和 java.util.Date),但由于公共 API 的性质,永远不能再删除(仅弃用)。

