Search code examples
design-patternsjdbcbridge

Why is JDBC a typical application of Bridge design pattern?


I found from lots of resources that JDBC is a typical example of Bridge design pattern. But they usually didn't tell the details, so I would like to know the details. According to my understanding:

  • Driver interface is the bridge between the DriverManager and the concrete JDBC driver classes
  • Connection interface is the bridge between the Driver and the concrete JDBC connection classes
  • Statement interface is the bridge between the Connection and the concrete SQL statement classes
  • ResultSet interface is the bridge between the Statement and the concete result set classes

Please modify if the my statements are wrong. Also I guess DataSource interface is also a bridge, but I can not figure out that is a bridge between which classes


Solution

  • The Bridge pattern, as defined by the "Gang of Four", means decoupling an abstraction from its implementation so that the two can vary independently. In this context, it's not so much about bridging different classes together. Instead, the pattern illustrates the Open/Closed principle where the interface (JDBC API) stays the same, but new implementations (JDBC drivers) can be added and substituted for each other.

    What this means is that data access code using JDBC only needs to depend on the API interfaces such as Connection, Statement or ResultSet, instead of caring about the actual database system the application is connected to. JDBC will bridge the application to the database that's used in the environment the application is deployed to. For this reason, you can run the same code (using JDBC abstractions) against different RDBMS, and only the JDBC driver (implementation) needs to change.

    Added by RUI: http://www.informit.com/articles/article.aspx?p=29302