The implementation of the event source is not much more complex:

public class StockPricer {
  java.util.Vector listeners = new java.util.Vector ();

  public StockPricer () {
  }
  public synchronized void addStockListener (StockListener listener) {
    listeners.addElement (listener);
  }
  public synchronized void removeStockListener (StockListener listener) {
    listeners.removeElement (listener);
  }
  public void broadcastStockEvent(StockEvent ev);{
    java.util.Enumeration e = listeners.elements()
    while( e.hasMoreElements() )
      ((StockListener) e.nextElement()).stockPriceUpdated(ev);
    }
}

This implementation uses a Vector to store the list of listeners. In order for this to be recognized as an event source, only the addStockListener and removeStockListener methods must be declared. The broadcastStockEvent method is a convenience method we created to send the event to all of the listeners. Another useful method you might use exposes the listeners as a property:

  public synchronized StockListener [] getStockListeners () {
    StockListener [] ret = new StockListener [listeners.size ()];
    listeners.copyInto (ret);
    return ret;
  }
 
loading table of contents...