Bridge Pattern

Split one class family that varies in two directions into two hierarchies joined by composition, so each side can grow without multiplying the other.

Overview

The problem it solves A hierarchy starts to vary in two directions at once. Shapes come in circles and squares; they can be drawn as vectors or as pixels. With inheritance alone you end up with VectorCircle, RasterCircle, VectorSquare, RasterSquare, and every new shape or renderer multiplies the list. The Bridge pattern cuts the family in two. One hierarchy holds what the object is (the abstraction: shapes). The other holds how the work is done (the implementor: renderers). The abstraction holds a reference to an implementor and delegates the low-level steps to it.

The pattern is a structural pattern from the Gang of Four book (Gamma, Helm, Johnson and Vlissides, *Design Patterns*, 1994). The book's roles are Abstraction, RefinedAbstraction, Implementor and ConcreteImplementor.

  • 3 — 3 — 9 — 6
  • 5 — 4 — 20 — 9
  • 6 — 6 — 36 — 12

The third column is shapes times renderers. The fourth is shapes plus renderers. Adding one renderer costs one class instead of one per shape.

Which side does what The abstraction speaks the client's language: draw, resize, move. The implementor offers primitives: draw a circle of this radius, fill a square of this side. The abstraction's methods are written once in terms of those primitives, and the implementor decides what they mean.

  • Abstraction (Shape, Circle, Square) — High-level operations the client calls — The domain grows: a new shape, a new operation
  • Implementor (Renderer, Vector, Raster) — Low-level primitives — The platform changes: a new output format or device
  • The bridge (the field renderer) — One reference — Never; it is the seam

A runnable example Any shape works with any renderer, and the renderer can be swapped on a live object.

Adding a Triangle touches the abstraction side only; adding a PdfRenderer touches the implementor side only. Neither addition edits the other side, which is the test that you have a bridge and not just a helper object.

A bridge in the wild: JDBC JDBC is the example most often given. Application code programs against the java.sql interfaces: Connection, Statement, ResultSet. Each database vendor ships a driver that implements them. DriverManager finds drivers through the jdbc.drivers system property or the service-provider mechanism (META-INF/services/java.sql.Driver) and picks one for the URL you pass to getConnection. The API side and the driver side evolve separately, and your code never names a vendor class. The Java docs also point to DataSource as the preferred way to obtain connections, which keeps the same separation.

Be precise about where the bridge is, because an interviewer may push. The strict bridge is DriverManager (the abstraction, whose getConnection is written over the implementor primitive Driver.connect). Connection and the rest are interfaces the driver implements directly, with no second hierarchy above them; that part is a plain interface with pluggable implementations. Saying so shows you know the pattern's test, not just its famous example.

Bridge next to its look-alikes | Pattern | Two hierarchies? | When it is introduced | Intent | |---|---|---|---| | Bridge | Yes, both can grow | Designed up front | Let abstraction and implementation vary independently | | Adapter | No, one wrapper | After the fact | Make an existing class fit an interface | | Strategy | One context, many algorithms | Any time | Swap one algorithm behind one slot | | Abstract Factory | Creates families of objects | Any time | Often used to create the implementor for a bridge |

What you will be able to do - Explain the M-times-N explosion with numbers and show how a bridge makes it M-plus-N. - Draw both hierarchies and point to the one field that joins them. - Write a bridge in Python and prove with asserts that any abstraction works with any implementor. - Tell Bridge from Strategy and from Adapter by intent and timing. - Recognize the shape in JDBC and in platform-independent toolkits.

Interview questions

  • What is the structure of the Bridge pattern, and where exactly is the bridge?
  • How is Bridge different from Strategy?
  • How is Bridge different from Adapter?
  • Show the smell that Bridge removes, and the fix.
  • Who creates the implementor, and can it change at run time?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio