title: Implementing Event-Generator Idiom in Java
body: <p>I'm trying to implement the Event Generator Idiom (<a href="http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.html" rel="nofollow noreferrer">http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.html</a>). I find things to be a bit "odd" when it comes to the observable class though. Let's say I have the following classes:</p>
  
  <pre>
  interface BakeryListener  
  + orderReceived(BakeryEvent event)  
  + orderProcessing(BakeryEvent event)  
  + orderFinished(BakeryEvent event)  
  + orderDelivered(BakeryEvent event)  
  
  LogView, OrderReadyView etc. implements BakeryListener  
  Creates a GUI for each own use
  
  Order  
  VO / DTO object, used as the source in BakeryEvent 
  
  BakeryDAO (the observable)  
  - orders : Vector  
  - listeners : Vector  
  + takeOrder, cancelOrder, bake and other regular DAO methods, 
  triggering an event by calling fireEvent
  + addBakeryListener(BakeryEvent event)  
  + removeBakeryListener(BakeryEvent event)  
  - fireEvent(Order source, EVENTTYPE????)
  
  BakeryGUI  
  Creates/gets a reference to BakeryDAO. Creates and attaches LogView, OrderReadyView as listeners on BakeryDAO.
  </pre>
  
  <p>In the link I gave initially he recommends to "Name the event propagator method fire[listener-method-name].". I find this to be redundant: making a snapshot and iterating over the listeners in each fire-method, when the only thing changing is which method to call on the interface. Thus I have made a single fireEvent method, and it is working sort of. The problem is to keep the datatype of the event parameter in fireEvent "in sync" with the methods defined in BakeryListeners. Currently the fireEvent looks like this (excerpt):</p>
  
  <pre>
  for(BakeryListener listener : copyOfListeners){
      if(eventType.equals("received")) listener.orderReceived(event);
      else if(eventType.equals("processing")) listener.orderProcessing(event);
  }
  </pre>
  
  <p>... etc. I guess I could use an enum instead of a string to make it impossible to call fireEvent with an eventType that doesn't exist, but I still have to map Type.RECEIVED to listener.orderReceived etc?</p>
  
  <p>Is it possible for the fireEvent method to take the BakeryListeners methods as a parameter? I.e (pseudo-code) method declaration:  </p>
  
  <pre><code>fireEvent(BakeryListeners.methods eventType, Order source)
  </code></pre>
  
  <p>and then just call the appropriate method directly inside fireEvent(without if/switching):  </p>
  
  <pre><code>call(listener, eventType( source)) 
  </code></pre>
  
  <p>Then it also would be impossible to create a event who isn't defined in the interface BakeryDAO.takeOrder() -> fireEvent(eventWhoDoesntExist) -> exception?</p>
  
  <p>Is it possible to do this in Java? Or a better way if I've understood things wrong?</p>