Simpler DriverManager

Listing 1. Calling the getConnection() method scans the static data structure and calls the connect() method of each registered driver, passing the URL string as an argument. The connect() code in each driver parses the URL and decides whether or not it is the correct driver to handle that URL. If it is, it creates a Connection object and returns a reference to it; otherwise, it returns null. The DriverManager class (a simplified example is shown here) locates the correct driver, and when one of the drivers returns a Connection object, DriverManager knows it has found the correct driver for that URL. For clarity the real code includes some exception handling and other omitted items.

public class DriverManager
{
/**
   * Driver references are kept here.
   */
   private static Vector drivers = new Vector();

/**
   * This registers a Driver.
   */
   public static void registerDriver(Driver driver)
   {
      drivers.add(driver);
   }

/**
   * Locates the correct Driver to handle the URL.
   */
   public static Connection getConnection(String url)
   {
      for (Iterator it = drivers.Iterator();
         it.hasNext();)
      {
         Driver d = (Driver)it.next();
         Connection c = d.connect(url);
         if ( c != null )
         {
            return c;
         }
      }
   }
   ...