Personalized Event Listeners in Java

Although the Observer pattern is implemented natively in Java, sometimes we need to make an event management that suits better our needs.

The problem of event handling is very simple: We have an object that will be changing its state. Without touching its code, we should be able to “hook” to other objects that are pending status changes and act accordingly. This “hook” must be turned on and off dynamically at runtime.

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, and notify relevant changes to the objects concerned. The class is used to pass information during an event. The interfaces will make the distinction between objects observed and observable objects and any other objects on the application.

But the best way to see how it works is through an example:

The instances of the class will be those that carry MyCustomEvent information to an observable object from the observed object when an event occurs. This class should contain all the necessary information on that event. For example, if the event was a mouse click, this class should carry data such as coordinates on the screen or the number of clicks. Specifically, our event class contains only the original object that triggered the event (the observable object).

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 which will keep the relations between observers and observables will be called 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);
  }
}

Both objects (observable objects as observers) must implement the interface MyCustomListener. This interface requires implementing the function fire(MyCustomEvent event) . In the observable, this function will be responsible for calling MyCustomEventHandler. For observers, this function will be called when the observed object launches an event.

Actually it would be convenient to separate the interface into two: one for observers and another for observables, because 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);
}

If we have different types of events (similar to how the MouseListener), all we need to do is to expand MyCustomListener interface with all the functionality we want to have:

import java.util.EventListener;

public interface MyCustomListener extends EventListener {
  public void fireObjectRecycled (MyCustomEvent event);
  public void fireObjectRefreshed (MyCustomEvent event);
  public void fireObjectUpdated (MyCustomEvent event);
}

So, we might use different classes derived from MyCustomEvent for each of these features.

The static object MyCustomEventHandler also need to be modified to launch the different events.

References: thread which discusses this type of implementation of Event Handler

Leave a Reply

Your e-mail address will not be published. Required fields are marked *