简体   繁体   English

Java中的事件侦听器

[英]Event listener in java

We desing our application with base classes and Event Listener approch, thus the base class and a event listener interface. 我们使用基类和事件侦听器方法设计应用程序,从而使用基类和事件侦听器接口。 base class invoke appropriate event listener method after call any operation. 基类在调用任何操作后调用适当的事件侦听器方法。 Following code shows my design : 以下代码显示了我的设计:

import java.util.EventListener;

public interface MyEventListener extends EventListener
{
    public void preOperation();

    public void postOperation();
}

Then I implement this interface as following : 然后,我实现此接口,如下所示:

class MyEventListenerImpl implements MyEventListener
{
    @Override
    public void postOperation()
    {
        System.out.println("Do previous operation");
    }

    @Override
    public void preOperation()
    {
        System.out.println("Do post operation");
    }
}

Then I write base class as following : 然后,我将基类编写如下:

abstract class Base
{
    private MyEventListener eventListener; /* this member injected */

    public abstract void operation_1();

    public void doOperation_1()
    {
        eventListener.preOperation(); /* call listener before invoking main operation_1 implementation */

        operation_1(); /* call main operation_1 implementation */

        eventListener.postOperation(); /* call listener after invoking main operation_1 implementation */
    }
}

after these works I write my implementation of Base class and implement operation_1 method. 完成这些工作后,我将编写我的Base类的实现并实现operation_1方法。 I do this approach by java.util.EventListener marker interface but after design my problem see another class : 我通过java.util.EventListener标记接口执行此方法,但是在设计我的问题后,请参见另一个类:

  1. Abstract class EventListenerProxy in java.util package. java.util包中的抽象类EventListenerProxy
  2. EventListenerSupport generic class in org.apache.commons.lang3.event package. org.apache.commons.lang3.event包中的EventListenerSupport通用类。

I don't know using these class never. 我不知道永远不要使用这些类。 I want know two things: 我想知道两件事:

  1. Your opinions about my design.(good or bad) 您对我的设计的看法(好坏)
  2. best practice for Event Listener approach.(by said class or any third party frameworks) 事件侦听器方法的最佳实践(通过所述类或任何第三方框架)

It's hard to discuss about a particular design without knowing which problem it's supposed to solve. 在不知道应该解决哪个问题的情况下,很难讨论特定的设计。

Anyway, the principal problem with your design is that you can only have one listener. 无论如何,设计的主要问题是只能有一个监听器。 Using EventListenerSupport would allow easily supporting several ones, and would make the add/remove listener methods trivial to implement. 使用EventListenerSupport可以轻松支持多个方法,并且可以轻松实现添加/删除侦听器方法。

Another problem is that your listener methods don't take any event as parameter. 另一个问题是您的侦听器方法不将任何事件作为参数。 This makes it impossible for a single listener to listen to several Base instances, because it can't use the event to detect the source of the event. 这使得单个侦听器无法侦听多个Base实例,因为它无法使用事件检测事件的来源。

An EventListener, by nature, can not know that something will happen, because it is triggered from inside operation_1 in your example. 本质上,EventListener不知道发生什么事情,因为它是从示例中的operation_1内部触发的。

What you are looking for are AOP method interceptors. 您正在寻找的是AOP方法拦截器。 Especially aopalliances MethodInterceptor interface will be useful to you: 特别是aopalliances的MethodInterceptor接口将对您有用:

public Operation1Interceptor implements MethodInterceptor {

  Object invoke(MethodInvocation invocation) throws Throwable {

    /* feel free to access the arguments, if you wish */
    foo(invocation.getArguments())

    Object retval = invocation.proceed();

    /* Log it or do whatever you want */
    bar(retval);

    baz();

    return retval;
  }

}

The easiest way to use that is Google Guice, but there are some tutorials around. 最简单的使用方法是Google Guice,但这里有一些教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM