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.
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.
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).
implements is for interfaces - but Observable is a class, not an interface (my previous comment should say Observable instead of Observer, sorry - I will delete it and re-post it below as it's too confusing otherwise).Observable, because my class already has a superclass - Java only allows a single superclass.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)