Java events and event listeners [closed]

亡梦爱人 提交于 2019-12-10 14:19:50

问题


I searched on Google to learn how to create custom events and event listeners. After reading some articles about it, I'm still confused. I would like to ask you guys for a review of the ways for making custom events (non-GUI related events) and handlers. Can some provide a simple explanation on how to create custom events and listeners?


回答1:


You basically create an interface as the listener such as

public interface EatListener {

    ...

}

inside the EatListener class, you have method that you call with the event as the method's parameter; such as

public void onEat(EatEvent);

then you can have a class like Human that implements or instantiates an EatListener; such as

public class Human {

    private EatListener listener;

    public void eatFood(Food food) {
        if(listener != null) {
            listener.onEat(new EatEvent(food));
        }
    }

}

then you need to have the actual EatEvent; which can be as simple as wrapper for food with possibly some extra data.

As with any GUI on java you can create anonymous inner classes from that interface:

new EatListener() {
    public void onEat(EatEvent event) {
        System.out.println("I just ate " + event.getFood().getName());
    }
}



回答2:


I've written lots of event based systems (non-gui) and there are many gotchas in implementing your own. Some of the classic things are threading and memory leaks.

Threading/process control simply means that when you fire an event, when does the listener get invoked - immediately or later on? If you fire immediately, you can end up with a really poorly performing system that is constantly reacting to an event that changes immediately after the listener has been invoked. There is no easy answer here, and it really depends on your need. In general, if you can defer the firing of an event you will have a better performing system (because it might avoid calling the listener many times for the same event - or a cycle of events that leads to the same event)

The second big gotcha is memory leaks. The lack of delete in Java is a lovely thing, but listeners are a giant gun that is strapped to your foot. If you have a listener that is attached to a instance that uses a load of memory, as long as another object contains a reference to that listener, that memory is going to be hanging around. There are a bunch of solutions to this, such as WeakReferences but in general, you need to be pretty careful and examine the number of listeners when you are testing your application and make sure they detach (unlisten? :) ) gracefully.

In short, if I were you, I would consider using something like : http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/eventbus/package-summary.html http://codingjunkie.net/guava-eventbus/ which has been developed with many of these issues in mind.



来源:https://stackoverflow.com/questions/12898485/java-events-and-event-listeners

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