开发者

Is java.util.Observable used anywhere?

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".

Whereupon he floored me by pointing out that there would have been a name clash with java.util.Observer and java.util.Observable, which have been around since JDK 1.0. I'd never heard of them before.

There are no usages listed in the JavaDocs and google/googlecode don'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 th开发者_StackOverflow中文版em useful?


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.

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).


Observer and Observable are 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.. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜