Quote:Original post by Emmanuel Deloget
Signal/slot and observers are totally different beasts, and they are used to solve different problems. Basically, you use an observer when you want to emphasis on the fact that an update is needed, while the signal/slot mecanism puts an emphasis on the treatment itself and on the fact that it is possible to modify the dynamically change the treatment.
"Totally different beasts"? IMO, they are just two types of event handling mechanisms that both try to decouple the sender from the receiver - with signals/slots, the sender of the signal does not know to which slots it goes, and what those slots do. With observers, the "sender" has a list of receivers (observers) and calls a method on each, but does not know what those methods do.
And you can dynamically add/remove observers from those lists, too.
Maybe our ideas of what an Observer is are different? I thought that classes implementing Java's MouseListener, ActionListener, ... interfaces were observers, because they observe what happens to the GUI (that a button is clicked, etc.)
Quote:
Moreover, signals are typically functions or methods while the observer's update function is inherited from a base class.
Java doesn't have an easy way to use "free" methods/functions as first-class objects - using the reflection API to get at a method just feels inelegant, clunky and error-prone (because you throw compile-time checks out of the window) to me. Using anonymous classes implementing an interface is much easier.
That why I just don't see any benefits to using signals/slots
in Java (compared to languages without anonymous classes or delegates or closures, like C++).