Signals and Slots

Started by
22 comments, last by Emmanuel Deloget 18 years ago
Quote:Original post by smr
Seems complicated (of course everything in java seems too complicated to me ;P). Something like this would be cool (I don't know if this would work. I'm not a java programmer):

*** Source Snippet Removed ***


It's nearly that simple in C#:

delegate void ClickDelegate();class X {    public event ClickDelegate Click;}class Y {    public void OnClick() { /*do stuff*/ }}void settingItUp() {    X x = new X();    Y y = new Y();    x.Click += new ClickDelegate(y.OnClick);}


EDIT: I'll stop posting about C# in this topic. Promise.

[Edited by - dalleboy on April 12, 2006 10:56:34 AM]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Advertisement
Quote:Original post by Barius
I don't really see how signals/slots are useful in Java. I haven't used them, but it seems to me that they are mainly useful in a language that makes it painful to use observer classes, because those
a) need to be defined at a separate location in the code, making the code hard to follow
b) need to be initialized explicitly with "context variables" (what objects to call methods on, etc)
But in Java, you can use anonymous inner classes, which can be used in-place and capture their context automatically - they are effectively closures.


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. Moreover, signals are typically functions or methods while the observer's update function is inherited from a base class.

Regards,
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++).
Quote:Original post by Barius
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.)


I was perhaps too extreme. The biggest difference between these two solution is semantic rather than practical. The purpose of the observer pattern is to manage state change, while the signal/slot mecanism is more like a generic way to ask to a target to perform a particular action. The difference is easier to see when you come to the implementation: typically, your observer's update() method is the only entry point of your observer - it will probably have a state parameter (or the update method will get the state of the subject) in order to perform a specific action. Using the signal/slot, you'll define 1+ independent signals - so you have 1+ entry points that are used to perform specific actions. In one case, the observer does the dispatch, in the other case, the signal emiter does the dispatch.

Quote:Original post by Barius
Quote:Original post by Emmanuel Deloget
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++).


I get your point: of course, if the Java language itself makes it difficult and error prone to use the signal/slot mecanism, I would suggest to not using it at all unless it is really necessary (something that is never true: I never saw a case where you have only one design choice).

This topic is closed to new replies.

Advertisement