Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to callback to multiple listeners?

Tags:

java

callback

Is it possible to set multiple listeners using callbacks?

I am trying to understand how the callbacks work and I am trying to figure out it that's what I need.

I have one UDP messaging class which receives/sends messages. When I parse certain messages, I would like to update multiple UI classes.

At the moment I have something like this:

class CommInt {

    private OnNotificationEvent notifListener;

    public setNotificationListener(OnNotificationEvent notifListner) {
        /* listener from one UI */
        this.notifListener = notifListener;
    }

    private parseMsg(Message msg) {

        if (msg.getType() == x) {
            notifListener.updateUI();
        }

    }

}

I need to update another UI as well. The other UI will use the same interface but the body will be different.

How is it possible call all listener which are implemented from that interface?

like image 826
Arturs Vancans Avatar asked Oct 26 '25 10:10

Arturs Vancans


1 Answers

It's very likely that you will need to make a List of listeners, then iterate through each one calling updateUI(). Of course, you also need an addListener() method so that you don't overwrite.

class CommInt {

  private List <OnNotificationEvent> listeners= new ArrayList <OnNotificationEvent>();


  public void addNotificationListener(OnNotificationEvent notifListner) {
    listeners.add (notifListener);
  }

  private void parseMsg(Message msg) {

    if (msg.getType() == x) {
      for (notifListener : listeners){
        notifListener.updateUI();
      }
    }

  }

}
like image 81
A--C Avatar answered Oct 28 '25 23:10

A--C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!