Aunque el patrón Observer está implementado nativamente en Java, sometimes we need to make an event management that suits better our needs when using event listeners.
Some context
El problema del manejo de eventos es muy sencillo: Tenemos un objeto que va a ir cambiando de estado. Sin tocar su código, debemos ser capaces de "engancharlo" a otros objetos para que estén pendientes de estos cambios de estado y actúen en consecuencia. Este "enganche" debe poder activarse y desactivarse dinámicamente durante la ejecución.
To implement it we will use a static object, a class and an interface. The static object will be responsible for ensuring the relationships between observers and observables. It will also notify relevant changes to the objects concerned. To pass information during an event we use that class. The interfaces will make the distinction between objects observed and observable objects and any other objects on the application.
A real example
But the best way to see how event listeners works is through an example:
Las instancias de la clase MyCustomEvent serán las que porten la información de un observado a un observable cuando se produzca un evento. Esta clase deberá contener toda la información necesaria sobre ese evento. Por ejemplo, si el evento fuera un click del ratón, esta clase debería llevar datos tales como: coordenadas en la pantalla o el número de clicks. En concreto, nuestra clase de evento sólo contiene el objeto original que lanzó el evento.
import java.util.EventObject;
import java.util.LinkedList;
import java.util.List;
public class MyCustomEvent extends EventObject {
private static final long serialVersionUID = 7383182229898306240L;
private final MyCustomListener source;
public MyCustomEvent (MyCustomListener OriginalSource) {
super (OriginalSource);
this.source = OriginalSource;
}
MyCustomListener public getSource () {
this.source return;
}
}
The static object that keeps the relations between observers and observables is MyCustomEventHandler. It is very important to use synchronization methods to avoid concurrency issues in multithreaded applications:
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyCustomEventHandler {
private static final Log log = LogFactory.getLog(MyCustomEventHandler.class);
private static Map <MyCustomListener, Set > = new LinkedHashMap observable <MyCustomListener, Set > ();
public static void fire (MyCustomEvent event) {
log.trace ("fire (" + event + ")");
try {
Observers = getRemarks September (source);
if (Observers! = null) {
for (final MyCustomListener pl: Observers) {
try {
pl.fire (event);
} catch (Throwable t) {
log.error (t, t);
}
}
}
} catch (Throwable t) {
log.error (t, t);
}
}
private static synchronized September getRemarks (MyCustomListener source) {
observables.get return (source);
}
/ **
* Register to watch to alert you when observable change
*
* @ Param observer
* @ Param observable
* /
public static synchronized void register (MyCustomListener observer, MyCustomListener observable) {
Observers getRemarks September = (observable);
if (null == observers) {
observers = new HashSet();
}
observers.add(observer);
observables.put(observable, Observers);
}
/ **
* Deregister not become observers to alert you when observable
* Modify
*
* @ Param observer
* @ Param observable
* /
public static synchronized void deregister (MyCustomListener observer, MyCustomListener observable) {
Observers getRemarks September = (observable);
if (null == Observers) {
Observers = new HashSet ();
}
observers.remove (observer);
observables.put (observable, Observers);
}
}
Tanto los objetos observables como los objetos observadores tienen que implementar la interfaz MyCustomListener. Esta interfaz obliga a implementar el método fire(MyCustomEvent event)
. En los observables, esta función será la encargada de llamar a MyCustomEventHandler. En los observadores, esta función será llamada cuando se lance un evento de su objeto observado.
Actually, it would be convenient to separate the interface into two: one for observers and another for observables. If an object wants to be both observer and observed, it may have problems with this system. But for simplicity for the example we have chosen this implementation.
import java.util.EventListener;
public interface extends EventListener {MyCustomListener
public void fire (MyCustomEvent event);
}
Si quisiéramos tener diferentes tipos de eventos (parecido a como funciona el MouseListener), lo único que necesitaríamos es ampliar la interfaz MyCustomListener con todas las funcionalidades que queremos que tenga:
import java.util.EventListener;
public interface MyCustomListener extends EventListener {
public void fireObjectRecycled (MyCustomEvent event);
public void fireObjectRefreshed (MyCustomEvent event);
public void fireObjectUpdated (MyCustomEvent event);
}
Así mismo, podríamos usar diferentes clases derivadas de MyCustomEvent para cada una de estas funcionalidades.
We have to modify the static object MyCustomEventHandler to launch the different events.
Referencias: Hilo donde se discute este tipo de implementación de Event Handler