Reputation: 430
Why do a lot of people dislike the JDK Observer pattern and suggest to implement your own? Why re-invent the wheel?
The re-implemented observer that I see is same observer of the JDK.
Upvotes: 1
Views: 372
Reputation: 42586
One possible reason is that Observable
is a concrete class that you must subclass. Java only has single inheritance, so if you already have a superclass you can't subclass Observable
as well.
A second reason is that you often want to add multiple types of observer (listener) to an object, and Observer
doesn't support this directly (you could fire different objects in the notifyObservers
method, but that's not as clear as having multiple listener interfaces, and has a lot of potential for error and inefficiency since observers would receive objects not intended for them).
Upvotes: 4
Reputation: 5090
java.util.Observable is a class. not an interface As such it provides some functionality without you having to code it yourself, but it means that your own class which extends it, cannot extend any other class. This is a limitation.
Other patterns have Observable as an interface and do not have that issue (but have the cost of you having to implement some more code yourself)
Upvotes: 1