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 created to send the event to all 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;

Copyright © 1997, 2013 Oracle and/or its affiliates. All rights reserved. Legal Notices