Java - signal / slot mechanism

For someone relatively new to the Java ecosystem, there is a pretty easy way to make frameworks like Qt and Django with your own signaling / receiver system, where components can say “I'm doing something” and other components can handle it quite loosely coupled?

I apologize in advance if this question has not passed the test of the “single objective answer”.

Edit: To add a little more context, this is related to the database-driven application tier for the web service. Certain resources, when they are saved, must also save an audit record containing additional contextual information. In Django, I would do this using a signaling mechanism or use one of several existing libraries that do just that. For the Scala program, I did my own hack thing using callback functions, but this is easier with first class functions. I have no doubt that frameworks like Swing provide opportunities for this kind of thing, but I (probably unreasonably) try to add this dependency to the fact that the application is currently quite vanilla (not that Django is not massive dependency on vanilla Python!)

+5
source share
4 answers

In Java, at least on the user interface side, this is usually done using the listen method : you register a listener method that is called when certain events occur. It may be slightly less loosely coupled than the Qt signal / slot mechanism, though ... Another approach you might consider is the java messaging service, which is specifically designed for loosely coupled communications in client-server systems based on the Java EE standard.

+3
source

Java, /, Qt, sig4j. , .

. :)

+3
+2

, , Broadcastreceiver:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUI();
    }
};

@Override
public void onPause() {
    super.onPause();
    getActivity().unregisterReceiver(broadcastReceiver);
}

@Override
public void onResume() {
    super.onResume();
    getActivity().registerReceiver(broadcastReceiver, new IntentFilter(Constants.ACTION_UPDATE_PROFILE_UI)); // the constant is to identify the type of signal, it can be any string you want
}

, , :

Intent signalIntent = new Intent(Constants.ACTION_UPDATE_PROFILE_UI);

, , :

sendBroadcast(signalIntent);

:

https://github.com/nickfox/Update-Android-UI-from-a-Service

0
source

All Articles