[Kde-java] Signals and Slots implemented in Java
Marco Ladermann
Marco.Ladermann at gmx.de
Thu Oct 9 21:47:08 CEST 2003
Because I don't like the signal-slot mechanism, where all the signals and
slots are identified by strings, I thought about the possibility of a pure
Java implementation of it. I don't know if the results are of any use for
QtJava, but maybe someone finds them interesting anyway.
Here is the outcome of my meditation:
1) Signals can be represented by interfaces extending a tagging interface
(Signal) and declaring exaclty one method: the signal itself.
2) Slots are objects of an anonymous class extending a tagging class (Slot)
that define a single method: the slot.
3) Signals and slots are connected by source object, signal class and target
slot object.
4) Signals are emitted by first calling the emit method with the signal as
parameter, which returns a proxy object implementing the signal's interface.
On this proxy object the signal method can be called, that propagates all
parameters to the connected slot objects.
Pros:
Signals and slot are made explicit. They can be found in the javadoc.
Access modifiers are naturally respected.
Signal and slot names can't be misspelled, IDE or javac will detect that.
Cons:
Syntax is different from C++ Qt
Runtime issues because of Proxy usage?
An example:
Declare the signal in one class and emit it:
class Foo extends Qt {
...
public interface MySignal extends Signal {
void sendOut(String hello);
}
...
((MySignal) emit(MySignal.class)).sendOut("Hello, Slots!");
...
}
Define a slot in another class:
class Bar extends Qt {
...
public Slot mySlot = new Slot() {
void receive(String str) {
System.out.println(str);
callMethod_in_enclosing_Object(str);
}
}
...
}
Now in a third class connect signal and slot:
Foo f = new Foo();
Bar b = new Bar();
connect(f, Foo.MySignal.class, b.mySlot);
Okay, that's it. Find enclosed the source and a little test. If it is worth a
comment, please, comment it!
Marco
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Qt.java
Type: text/x-java
Size: 10102 bytes
Desc: not available
Url : http://mail.kde.org/pipermail/kde-java/attachments/20031009/e1a986d9/Qt.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: QtTest.java
Type: text/x-java
Size: 1703 bytes
Desc: not available
Url : http://mail.kde.org/pipermail/kde-java/attachments/20031009/e1a986d9/QtTest.bin
More information about the Kde-java
mailing list