用于订阅的Java侦听器设计模式 [英] Java Listener Design Pattern for Subscribing

查看:186
本文介绍了用于订阅的Java侦听器设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我希望实现的基本功能:

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

// ......
}


// PLUGIN

public class PluginA
{
public PluginA()
{
//某种程度上以回调方式订阅Mainforms onProcessingCompleted myCallback()
}

public void myCallback(object sender)
{
}


}

我已阅读本网站: http://www.javaworld.com/javaqa/2000-08/01-qa-0804-events.html?page=1



他们参考手动实现整个订阅列表。但是代码不是一个完整的例子,我很习惯c#,我无法掌握如何在java中做到这一点。



有没有人有这个我可以看到的一个工作的例子



谢谢

Stephanie

解决方案

Java没有函数代理(有效的方法引用);你必须通过一个实现一个接口的整个类。例如

  class Producer {
//允许第三方插入监听器
ProducerEventListener my_listener;
public void setEventListener(ProducerEventListener a_listener){
my_listener = a_listener;
}

public void foo(){
...
//事件发生;通知监听器
if(my_listener!= null)my_listener.onFooHappened(new FooEvent(...));
...
}
}


//定义侦听器应该能够对
进行响应的事件public interface ProducerEventListener {
void onFooHappened(FooEvent e);
void onBarOccured(BarEvent e);
// ..在逻辑上需要多少;通常只有一个
}


//有些愚蠢的监听者对事件做出反应
class消费者实现ProducerEventListener {
public void onFooHappened(FooEvent e){
log.info(Got+ e.getAmount()+of foo);
}
...
}

...
someProducer.setEventListener(new Consumer()); //附加一个监听器的实例

通常你有一个通过匿名类创建的微不足道的听众:

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

如果要允许每个事件的多个监听器(如GUI组件),则可以管理一个列表您通常需要同步,并且具有 addWhateverListener removeWhateverListener 来管理它。



是的,这个 非常麻烦。你的眼睛不对你说谎。


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

解决方案

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.

这篇关于用于订阅的Java侦听器设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆