title: Easier Custom Events Handling in Java
body: <p>In Java, everytime I want to create a new custom event, I usually do it by add 3 methods namely: </p>
  
  <pre><code>addDogEventListener(EventListener listener); 
  removeDogEventListener(EventListener listener);
  dispatchDogEventListener(DogEvent event);
  </code></pre>
  
  <p>Then now if I want to dispatch another event, say CatEvent, I will have to create all these 3 methods again:</p>
  
  <pre><code>addCatEventListener(EventListener listener); 
  removeCatEventListener(EventListener listener);
  dispatchCatEventListener(CatEvent event);
  </code></pre>
  
  <p>Then if I want to manage just one kind of CatEvent event, say Meow, I have to copy and paste all these 3 methods again?! Like addCatMeowEventListener();... etc?</p>
  
  <p>And usually, I need to dispatch more than one kind of events. It will be very untidy to have the whole class filled with so many methods to transmit and handle the events. Not only that, these functions have very similar code, like loop through the EventListenerList, add event to the list, etc.</p>
  
  <p>Is this how I should do event dispatching in Java?</p>
  
  <p>Is there a way like I can do it like:</p>
  
  <pre><code>mainApp.addEventListener(CatEvent.MEOW, new EventHandler() { meowHandler(Event e) { });
  mainApp.addEventListener(CatEvent.EAT, new EventHandler() { eatHandler(Event e) { });
  myCat.addEventListener(DogEvent.BARK, new EventHandler() { barkHandler(Event e) { myCat.run() });
  </code></pre>
  
  <p>In this way, I can just handle the different types of CatEvent in different eventHandler class and functions and I don't have to keep creating different event listener methods for different events?</p>
  
  <p>Maybe I am missing something out about Java's event handling but is there a neater way that I don't have to keep copy and paste the 3 methods plus creating so many different kind of event objects for every different kind of methods I want to dispatch?</p>
  
  <p>Thanks!</p>