N
N
Nikita Permin2012-04-04 15:09:48
Java
Nikita Permin, 2012-04-04 15:09:48

Qt=> Java. Signals and slots

I'm new to Java, so I'm interested in the question: how can information be exchanged between objects of different classes in Java in the same way as signals and slots from Qt? I suppose, as an option, you can inherit from one custom class that would implement similar functionality, but it seems to me that this is not a very good option ... What can you suggest?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayorovp, 2012-04-13
@NekitoSP

What you're after is called the Observer Pattern. Signals in Qt are just one implementation of this pattern.
The standard Java implementation method is as follows:
1. An interface is created for the observer (a slot in Qt terminology) with the necessary methods
2. Subscription methods (connect in Qt), unsubscribe and call methods are created for the observable class.
Naming convention:
Let's say the signal is called Foo
Then the interface will be called FooListener, it will have a method foo
The subscribe method will be called addFooListener, the unsubscribe method will be removeFooListener, the call method will be fireFooListeners (the latter must be protected).
Yes, it turns out more code than in Qt. But that's the ideology of Java

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question