tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Design Patterns > Java Design Patterns > Facade : JDBC Driver Example

Facade : JDBC Driver Example 

Facade pattern comes under Structural design pattern. It provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Behaviour & Advantages

  • Simplifies the subsystem usage.
  • Acts as entry point to the subsystem.
  • Shields clients from subsystem components.
  • The interaction and communication among subsystems is hidden from client.
  • Facade
    Provides a simplified API to interact with subsystems. It knows how to handle a request and manages request processing with subsystems.
  • Subsystem classes
    Complex set of classes designed to perform a specific task.
  • Client
    One who uses Facade to interact with subsystems.

This example shows a JDBC Driver Manager Facade implementation which shields the clients from dealing with database specific classes. Here Oracle & MySQL Driver related classes represent two complex Subsystems. As soon as the driver class is loaded it gets registered with JDBC Driver Manager. Then Client can request the Driver Manager to get connection for a specific database.

Framework JDBC Driver interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/IJDBCDriver.java 
   
package com.bethecoder.tutorials.dp.facade2;

public interface IJDBCDriver {
  /**
   * Get database vendor name 
   */
  public String getDBVendor();
  
  /**
   * Get connection for this database.
   */
  public IConnection getConnection();
}
   

Framework JDBC Connection interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/IConnection.java 
   
package com.bethecoder.tutorials.dp.facade2;

public interface IConnection {
  public IStatement createStatement();
}
   

Framework JDBC Statement interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/IStatement.java 
   
package com.bethecoder.tutorials.dp.facade2;

public interface IStatement {
  public void executeQuery(String query);
}
   

Oracle JDBC Driver interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/oracle/OracleJDBCDriver.java 
   
package com.bethecoder.tutorials.dp.facade2.oracle;

import com.bethecoder.tutorials.dp.facade2.DriverManager;
import com.bethecoder.tutorials.dp.facade2.IConnection;
import com.bethecoder.tutorials.dp.facade2.IJDBCDriver;

public class OracleJDBCDriver implements IJDBCDriver {

  @Override
  public IConnection getConnection() {
    return new OracleConnection();
  }

  @Override
  public String getDBVendor() {
    return "oracle";
  }

  /**
   * Register this driver with Driver Manager.
   */
  static {
    DriverManager.registerDriver(new OracleJDBCDriver());
  }
}
   

Oracle JDBC Connection interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/oracle/OracleConnection.java 
   
package com.bethecoder.tutorials.dp.facade2.oracle;

import com.bethecoder.tutorials.dp.facade2.IConnection;
import com.bethecoder.tutorials.dp.facade2.IStatement;

public class OracleConnection implements IConnection {

  @Override
  public IStatement createStatement() {
    return new OracleStatement();
  }

}
   

Oracle JDBC Statement interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/oracle/OracleStatement.java 
   
package com.bethecoder.tutorials.dp.facade2.oracle;

import com.bethecoder.tutorials.dp.facade2.IStatement;

public class OracleStatement implements IStatement {

  @Override
  public void executeQuery(String query) {
    System.out.println("Oracle JDBC driver executing query : " + query);
  }

}
   

MySQL JDBC Driver interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/mysql/MySQLJDBCDriver.java 
   
package com.bethecoder.tutorials.dp.facade2.mysql;

import com.bethecoder.tutorials.dp.facade2.DriverManager;
import com.bethecoder.tutorials.dp.facade2.IConnection;
import com.bethecoder.tutorials.dp.facade2.IJDBCDriver;

public class MySQLJDBCDriver implements IJDBCDriver {

  @Override
  public IConnection getConnection() {
    return new MySQLConnection();
  }

  @Override
  public String getDBVendor() {
    return "mysql";
  }

  /**
   * Register this driver with Driver Manager.
   */
  static {
    DriverManager.registerDriver(new MySQLJDBCDriver());
  }
}
   

MySQL JDBC Connection interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/mysql/MySQLConnection.java 
   
package com.bethecoder.tutorials.dp.facade2.mysql;

import com.bethecoder.tutorials.dp.facade2.IConnection;
import com.bethecoder.tutorials.dp.facade2.IStatement;

public class MySQLConnection implements IConnection {

  @Override
  public IStatement createStatement() {
    return new MySQLStatement();
  }

}
   

MySQL JDBC Statement interface.

File Name  :  
com/bethecoder/tutorials/dp/facade2/mysql/MySQLStatement.java 
   
package com.bethecoder.tutorials.dp.facade2.mysql;

import com.bethecoder.tutorials.dp.facade2.IStatement;

public class MySQLStatement implements IStatement {

  @Override
  public void executeQuery(String query) {
    System.out.println("MySQL JDBC driver executing query : " + query);
  }

}
   

File Name  :  
com/bethecoder/tutorials/dp/facade2/DriverManager.java 
   
package com.bethecoder.tutorials.dp.facade2;

import java.util.HashMap;
import java.util.Map;

public class DriverManager {

  private static Map<String, IJDBCDriver> driverMap = new HashMap<String, IJDBCDriver>();
  private DriverManager() {
  }

  public static void registerDriver(IJDBCDriver driver) {
    driverMap.put(driver.getDBVendor(), driver);
  }
  
  public static IConnection getConnection(String database) {
    if (driverMap.containsKey(database)) {
      return driverMap.get(database).getConnection();
    }
    throw new IllegalArgumentException("No driver fournd for database : " + database);
  }
}
   

Facade usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/facade2/Test.java 
   
package com.bethecoder.tutorials.dp.facade2;

public class Test {

  /**
   @param args
   */
  public static void main(String[] argsthrows Exception {

    /**
     * The client doesn't need to know anything about
     * the subsystem implementation except initializing
     * driver class for required database.
     
     * Client just interacts with framework interface
     * rather than database specific classes.
     
     * Here DriverManager acts as a facade for the
     * underlying subsystem.
     
     * We can impose security checks or any other
     * access verifications in facade before
     * accessing the actual subsystem.
     */
    Class.forName("com.bethecoder.tutorials.dp.facade2.oracle.OracleJDBCDriver");
    IConnection connection = DriverManager.getConnection("oracle");
    IStatement statement = connection.createStatement();
    statement.executeQuery("select * from Employee");
    
    Class.forName("com.bethecoder.tutorials.dp.facade2.mysql.MySQLJDBCDriver");
    connection = DriverManager.getConnection("mysql");
    statement = connection.createStatement();
    statement.executeQuery("select * from Employee");
  }

}
   

It gives the following output,
Oracle JDBC driver executing query : select * from Employee
MySQL JDBC driver executing query : select * from Employee



 
  


  
bl  br