Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java.util.Observable used anywhere?

Tags:

java

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 them useful?

like image 325
MHarris Avatar asked Mar 04 '10 15:03

MHarris


People also ask

What is Java Util observable?

util. Observable is used to create subclasses that other parts of the program can observe. When an object of such subclass undergoes a change, observing classes are notified. The update( ) method is called when an observer is notified of a change.

What is the use of observe and observable in Java?

The Java language supports the MVC architecture with two classes: Observer : Any object that wishes to be notified when the state of another object changes. Observable : Any object whose state may be of interest, and in whom another object may register an interest.

Is observable a class or interface?

The Subject (Observable) is a class and must be subclassed. So you run into the problem that you cannot subclass from another more important class.

What is an observable class?

The java.util.Observable class represents an observable object, or "data" in the model-view paradigm.Following are the important points about Observable − The class can be subclassed to represent an object that the application wants to have observed. An observable object can have one or more observers.


2 Answers

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

like image 141
Péter Török Avatar answered Sep 19 '22 01:09

Péter Török


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

like image 27
Chris Dennett Avatar answered Sep 17 '22 01:09

Chris Dennett