Java Reference
In-Depth Information
Chapter 6. Bridge
The Bridge Pattern focuses on the implementation of an abstraction . Design Patterns
(Gamma et al. 1995) uses the word abstraction to refer to a class that relies on a set of
abstract operations, where several implementations of the set of abstract operations are
possible.
The ordinary way to implement an abstraction is to create a class hierarchy, with an abstract
class at the top that defines the abstract operations; each subclass in the hierarchy provides a
different implementation of the set of abstract operations. This approach becomes insufficient
when you need to subclass the hierarchy for another reason. It can also happen that the
abstract operations need to be defined and implemented in advance of abstractions that will
use them.
You can create a bridge by moving the set of abstract operations to an interface, so that an
abstraction will depend on an implementation of the interface. The intent of the B RIDGE
pattern is to decouple an abstraction from the implementation of its abstract operations, so that
the abstraction and its implementation can vary independently.
A Classic Example of Bridge: Drivers
A common use of B RIDGE occurs when an application uses a driver. A driver is an object that
operates a computer system or an external device according to a well-specified interface.
Applications that use drivers are abstractions: The effect of running the application depends
on which driver is in place. Each driver is an instance of the A DAPTER pattern, providing the
interface a client expects, using the services of a class with a different interface. An overall
design that uses drivers is an instance of B RIDGE . The design separates application
development from the development of the drivers that implement the abstract operations on
which the applications rely.
An everyday example of applications using drivers to operate computer systems appears in
database access. Database connectivity in Java usually depends on JDBC . ("JDBC" is a
trademarked name, not an acronym.) A good resource that explains how to apply JDBC is
JDBC Database Access with Java™ (Hamilton, Cattell, and Fisher 1997). In short, JDBC is
an application programming interface ( API ) for executing structured query language
( SQL ) statements. Classes that implement the interface are JDBC drivers, and applications
that rely on these drivers are abstractions that can work with any database for which a JDBC
driver exists. The JDBC architecture decouples an abstraction from its implementation so that
the two can vary independently—an excellent example of B RIDGE .
To use a JDBC driver, you load it, connect to a database, and create a Statement object:
Class.forName(driverName);
Connection c =
DriverManager.getConnection(url, user, passwd);
Statement s = c.createStatement();
Search WWH ::




Custom Search