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?
A source generates an event and sends it to one or more listeners. Listener simply waits until it receives an event. Listener processes events and then returns. User interface element is able to "delegate" the processing of an event to a separate piece of code.
The event listener is an object that is notified by the event source when and event occurs. The event listener receives an event object when it is notified of the event, then uses the object to respond to the event. The event source is required to provide methods that enable listeners to be registered and unregistered.
Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.
An EventListener interface defines the methods that must be implemented by an event handler for a particular kind of an event whereas an Event Adapter class provides a default implementation of an EventListener interface.
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());
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With