Java Listener Design Pattern for Subscribing

时光毁灭记忆、已成空白 提交于 2019-12-05 01:13:07

问题


I am trying to design a Java system that is simliar to the concept of c# delegates.

Here is the basic functionality i wish to achieve:

public class mainform
{
   public delegate onProcessCompleted
//......
    processInformation()
    {
            onProcessCompleted(this);
    }

//......
}


//PLUGIN

public class PluginA
{
        public PluginA()
        {
            //somehow subscribe to mainforms onProcessingCompleted with callback myCallback()
        }

        public void myCallback(object sender)
        {
        }


}

I have read through this site: http://www.javaworld.com/javaqa/2000-08/01-qa-0804-events.html?page=1

They make reference to implementing the whole 'subscription list' manually. But the code is not a complete example, and I'm so used to c# that I'm having trouble grasping how I could do it in java.

Does anyone have a working examle of this that I could see?

thanks
Stephanie


回答1:


In Java you don't have function delegates (effectively method references); you have to pass an entire class implementing a certain interface. E.g.

class Producer {
  // allow a third party to plug in a listener
  ProducerEventListener my_listener;
  public void setEventListener(ProducerEventListener a_listener) {
    my_listener = a_listener;
  }

  public void foo() {
    ...
    // an event happened; notify the listener
    if (my_listener != null) my_listener.onFooHappened(new FooEvent(...));
    ...
  }
}


// Define events that listener should be able to react to
public interface ProducerEventListener {
  void onFooHappened(FooEvent e);
  void onBarOccured(BarEvent e);
  // .. as many as logically needed; often only one
}


// Some silly listener reacting to events
class Consumer implements ProducerEventListener {
  public void onFooHappened(FooEvent e) {
    log.info("Got " + e.getAmount() + " of foo");
  }
  ...
}

...
someProducer.setEventListener(new Consumer()); // attach an instance of listener

Often you have trivial listeners that you create via an anonymous classes in place:

someProducer.setEventListener(new ProducerEventListener(){
  public void onFooHappened(FooEvent e) {
    log.info("Got " + e.getAmount() + " of foo");
  }    
  public void onBarOccured(BarEvent e) {} // ignore
});

If you want to allow many listeners per event (as e.g. GUI components do), you manage a list which you usually want to be synchronized, and have addWhateverListener and removeWhateverListener to manage it.

Yes, this is insanely cumbersome. Your eyes don't lie to you.



来源:https://stackoverflow.com/questions/4763225/java-listener-design-pattern-for-subscribing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!