Is java.util.Observable used anywhere?

前端 未结 2 1774
轮回少年
轮回少年 2020-12-24 06:36

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 Observa

2条回答
  •  伪装坚强ぢ
    2020-12-24 06:54

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

提交回复
热议问题