Quick revision
The short answer
The bridge pattern separates an abstraction from its implementation so that both can vary independently: clients program against one stable interface, and any number of implementations can plug in behind it. JDBC is a bridge because our code uses the java.sql interfaces while each database driver supplies the implementation, and SLF4J is one because we log through its API while a provider connects it to Logback, Log4j or another backend. Bridge is designed up front to allow several implementations; adapter is usually added later to make an existing class fit an interface it was not written for.
What to remember
In this answer, we saw that the bridge pattern keeps an abstraction and its implementations apart so each can change on its own. JDBC is a bridge between the java.sql interfaces and vendor drivers, and SLF4J is a bridge between its logging API and providers such as Logback or Log4j 2, both found at runtime through ServiceLoader. A bridge is planned up front for many implementations, while an adapter is added later to fit existing code into an interface. The price of a bridge is one more layer and an interface that must suit every implementation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The bridge pattern separates an abstraction from its implementation so that both can vary independently: clients program against one stable interface, and any number of implementations can plug in behind it. JDBC is a bridge because our code uses the java.sql interfaces while each database driver supplies the implementation, and SLF4J is one because we log through its API while a provider connects it to Logback, Log4j or another backend. Bridge is designed up front to allow several implementations; adapter is usually added later to make an existing class fit an interface it was not written for.
Why this question is asked
Bridge is one of the patterns we use every day without naming it. Every time we swap PostgreSQL for MySQL in a test, or switch a service from Log4j to Logback without touching a single log statement, we are relying on a bridge someone designed decades ago. Interviewers ask about it with adapter because the two look alike in a class diagram, and telling them apart shows whether we understand the intent behind a structure rather than only its shape. In this answer, we will recap what the pattern separates, walk through JDBC and SLF4J as real bridges, compare bridge with adapter, and look at what the extra layer costs.
1A Quick Recap of the Bridge Pattern
Every bridge has two sides. The abstraction is what client code talks to; it expresses operations in terms the application understands. The implementation is the low-level machinery that does the work, and there can be many of them. Between them is a reference, the bridge itself: the abstraction holds an implementation object and forwards work to it.
The point is independence. Because the client only sees the abstraction, we can add a new implementation without changing any client code. And because implementations only have to satisfy a narrow interface, the abstraction side can grow richer, with helper methods or subclasses, without forcing every implementation to change. The pattern is chosen on purpose, at design time, by someone who expects several implementations to exist.
- Application codeCalls the abstraction only
- Abstraction (API)Stable interface:
java.sql.Connection,org.slf4j.Logger - BridgeReference from API to the chosen implementation, found at runtime
- ImplementationPostgreSQL driver, MySQL driver, Logback, Log4j 2
4 layers, from the top down. Step through them one at a time.
2JDBC as a Bridge
JDBC defines interfaces in the java.sql package: Connection, PreparedStatement, ResultSet and others. The JDK ships the interfaces but no database code. Each vendor writes a driver that implements them for its own wire protocol. Our application asks DriverManager, or more often a DataSource such as HikariCP, for a Connection, and gets back an object of the driver's class. We never name that class.
Notice that the only thing that changed between databases was configuration. The driver is discovered through the ServiceLoader mechanism, which reads a file named META-INF/services/java.sql.Driver inside the driver jar. The abstraction, our code against java.sql, and the implementation, the driver, vary independently. New databases get JDBC support by writing a driver; nobody changes the JDK or our code.
3SLF4J as a Bridge
SLF4J, the Simple Logging Facade for Java, works the same way. Libraries and applications log through org.slf4j.Logger. At startup, SLF4J looks for a provider on the classpath and binds the API to it. Since SLF4J 2.0 this also uses ServiceLoader with an SLF4JServiceProvider; older 1.7 versions used a static binding class, so check your version if you see a warning about no providers being found.
Spring Boot uses this every day: its default starter brings Logback, and switching to Log4j 2 is a dependency change. Thousands of libraries can log through one API without caring which backend the final application chose. That is the reason both sides can vary: the library authors write to the abstraction, and the application owner picks the implementation.
4How Bridge Differs From Adapter
An adapter wraps an existing class so it fits an interface it was not written for. It is usually added after the fact, when we have two pieces of code that already exist and do not match. A bridge is planned from the beginning so that many implementations can be written against the same interface.
The SLF4J project actually shows both. The API with its providers is a bridge. But it also ships modules such as jcl-over-slf4j and log4j-over-slf4j, which take calls made to older logging APIs and redirect them into SLF4J. Those are adapters: they make existing code that calls Commons Logging fit the SLF4J world without changing it. Similarly, the old JDBC-ODBC bridge driver, despite its name, was an adapter of ODBC to the JDBC interfaces.
| Question | Bridge | Adapter |
|---|---|---|
| When is it introduced? | At design time, before implementations exist | Later, to connect existing code |
| What is its goal? | Let abstraction and implementation vary independently | Make one interface fit another |
| How many implementations? | Many, expected from the start | Usually one wrapped class |
| Who owns the interface? | The abstraction's designer | The client that needs it |
| JDK or library example | JDBC with drivers, SLF4J with providers | InputStreamReader, log4j-over-slf4j |
5The Cost of the Extra Layer
A bridge adds a level of indirection: every call passes through an interface, and when something goes wrong we look in two places, our code and the driver. More importantly, the interface must suit every implementation, which is hard. JDBC shows the strain. Databases differ in features, so the API grew escape hatches such as DatabaseMetaData to ask what the driver supports, vendor-specific unwrap() calls, and SQL text that is still written in each database's own dialect. A common interface tends towards the lowest common denominator, and the exceptions leak through.
So we choose a bridge when we are confident that several implementations will exist and that a shared contract can describe them well. When there will only ever be one implementation, the abstraction is overhead.
6Follow-up questions
Is SLF4J a facade or a bridge?
Both words fit, describing different things. From the client's point of view it is a facade: a simple API in front of a logging system. Structurally, the separation of a stable API from interchangeable providers chosen at runtime is a bridge. Design patterns describe intent, and one piece of software can serve several.
How does JDBC find the driver without our code naming it?
Since JDBC 4, DriverManager uses ServiceLoader to load every class listed in META-INF/services/java.sql.Driver inside the jars on the classpath. Each driver registers itself, and DriverManager asks each one whether it accepts the URL, so jdbc:postgresql: goes to the PostgreSQL driver. That is why Class.forName for drivers is no longer needed.
Can you give an example of an adapter in the JDK?
InputStreamReader adapts a byte-oriented InputStream to the character-oriented Reader interface, decoding bytes with a charset. Arrays.asList adapts an array to the List interface. In both cases the existing object was not written for the target interface, and a wrapper makes it fit.
7Conclusion
In this answer, we saw that the bridge pattern keeps an abstraction and its implementations apart so each can change on its own. JDBC is a bridge between the java.sql interfaces and vendor drivers, and SLF4J is a bridge between its logging API and providers such as Logback or Log4j 2, both found at runtime through ServiceLoader. A bridge is planned up front for many implementations, while an adapter is added later to fit existing code into an interface. The price of a bridge is one more layer and an interface that must suit every implementation.
Open with
The bridge pattern separates an abstraction from its implementation so the two can vary independently. Clients code against a stable interface, and several implementations can be plugged in behind it, usually chosen at runtime.
Then, if they want more
JDBC is a clear example: my code only uses the java.sql interfaces like Connection and PreparedStatement, and each database vendor supplies a driver that implements them. If I change the URL from PostgreSQL to MySQL and swap the driver jar, the code does not change.
SLF4J is the same idea for logging: libraries log through the SLF4J API, and the application picks a provider like Logback or Log4j 2 on the classpath. The difference from adapter is mainly intent and timing. A bridge is designed up front, expecting many implementations of one contract.
An adapter is added later to make an existing class fit an interface it was not written for, like InputStreamReader turning an InputStream into a Reader, or log4j-over-slf4j redirecting calls from an old API into SLF4J. The cost of a bridge is an extra layer of indirection and, more importantly, an interface that has to suit every implementation.
JDBC shows that tension, because databases differ and the API needs escape hatches like metadata queries and unwrap. So I use a bridge when I am confident several implementations will really exist.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the bridge pattern and show how JDBC or SLF4J fits it. How does bridge differ...": https://scrivio-interview-library.netlify.app/java/structural-patterns/64befe1d0d/