Why is JDBC a typical application of Bridge design pattern?

1.8k views Asked by At

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

2

There are 2 answers

2
Mick Mnemonic On BEST ANSWER

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

6
user207421 On

It isn't.

The Bridge pattern requires a concrete implementation of one API that maps to a concrete implementation of another API. It is rarely used: in fact I've used it exactly once in the 20+ years since the GoF book appeared, and I regretted that occasion.

JDBC provides abstract definitions of an API (interfaces) that are implemented by concrete implementations of the same API, and that in turn undertake network operations, rather than calling a different API.

However a Type 2 JDBC driver would internally be an example of the Bridge pattern. In this architecture the Java layer talks to a JNI layer which talks to a different C API probably already existing and provided by the vendor. This architecture was transitional and I doubt you will find an example now.