Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 57 ยท Structural Patterns

Bridge

Learn the Bridge design pattern: split an abstraction from its implementation so shapes and renderers vary independently and classes do not multiply.

The Bridge pattern splits one big class hierarchy into two independent ones, an abstraction and an implementation, connected by composition. Each hierarchy can then grow on its own without forcing changes (or new subclasses) in the other.

The problem it solves is the class explosion. Suppose you have shapes (circle, square) and ways to draw them (vector, raster). With plain inheritance you create VectorCircle, RasterCircle, VectorSquare, RasterSquare: four classes. Add a triangle and a third renderer and you need nine. The classes multiply as shapes x renderers. A bridge turns that multiplication into addition: shapes + renderers.

Multiplication becomes addition

Inheritance only 3 shapes x 3 renderers = 9 classes.
With a bridge 3 shapes + 3 renderers = 6 classes.

Every new shape or renderer adds one class, not a row of them.

1 / 2

The structure

The abstraction holds a reference to an implementor interface and delegates the platform- or vendor-specific work to it. Subclasses of the abstraction ("refined abstractions") add higher-level behavior; subclasses of the implementor supply concrete low-level behavior.

classDiagram
    class Shape {
        #renderer
        +draw()*
    }
    class Circle
    class Square
    class Renderer {
        <<interface>>
        +drawCircle(x, y, r)
        +drawRect(x, y, w, h)
    }
    class VectorRenderer
    class RasterRenderer
    Shape <|-- Circle
    Shape <|-- Square
    Renderer <|.. VectorRenderer
    Renderer <|.. RasterRenderer
    Shape o--> Renderer : bridge

Shapes decide what to draw; renderers decide how. The arrow between the hierarchies is the bridge.

Note that "abstraction" and "implementation" here have nothing to do with a Java abstract class or an interface versus a concrete class. The abstraction is the high-level control layer; the implementation is the layer that does the real work underneath.

Shapes and renderers

In this runnable version, renderers produce text descriptions instead of pixels, so the output shows exactly which side did what.

JS Shapes and renderers varying independently

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Two shape classes and two renderer classes produce four combinations, and none of those combinations needed its own class. Add a Triangle and it works with both renderers immediately; add an AsciiRenderer and it works with every existing shape (provided it implements the renderer interface).

Swapping the implementation at run time

Because the shape holds its renderer rather than being one, you can change the implementation while the program runs: print to a screen now, export to a file later. Inheritance cannot do that, because an object's class is fixed at creation.

JS Changing the bridge at runtime

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The Notifier class and its UrgentNotifier subclass vary along one axis (how the message is composed), while the devices vary along another (where it goes). Neither axis knows about the other beyond the small send(text) contract.

Where Bridge appears in practice

  • Cross-platform UI toolkits: a Window abstraction with refined subclasses (dialog, tool window) delegates drawing to a platform-specific peer for Windows, macOS or Linux.
  • Drivers: JDBC is a bridge. java.sql.Connection is the stable abstraction your code uses; each database vendor supplies the implementation.
  • Logging: a Logger abstraction (levels, formatting) writes through an appender or sink interface (file, console, network).
  • Remote controls and devices: a RemoteControl with BasicRemote and AdvancedRemote subclasses working with any Device (TV, radio).
  • Storage back ends: a Cache abstraction with a Backend that is in-memory or Redis.

Bridge versus Adapter and Strategy

Bridge, Adapter and Strategy all rely on composition and delegation, so they look alike in diagrams. Their intent differs:

  • Adapter fixes an incompatibility between two interfaces that already exist. It is applied after the fact.
  • Bridge is planned up front so that two dimensions of variation, the abstraction and its implementation, can evolve separately.
  • Strategy swaps one algorithm inside a single class; the context rarely has its own subclass hierarchy. Bridge involves two hierarchies, and its abstraction side usually has refined subclasses of its own.

If you find yourself writing RedCircle, BlueCircle, RedSquare, BlueSquare, you have two orthogonal dimensions (shape and color), and a bridge is the cure.

Pitfalls

  • Premature bridges: if there is only one implementation and no sign of a second, an extra interface is just indirection. Introduce the bridge when the second dimension of variation actually appears.
  • Chatty interfaces: if the abstraction makes dozens of tiny calls to the implementor, the contract is too low-level. Design the implementor interface around primitive operations that are meaningful and few.
  • Leaky abstraction: if shapes start checking renderer instanceof RasterRenderer, the separation is gone. Add the missing capability to the implementor interface instead.
  • Confusing the two hierarchies: keep the abstraction side about intent and the implementation side about mechanism.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What problem does the Bridge pattern primarily solve?

    Bridge separates an abstraction from its implementation so each can vary independently, replacing multiplication of classes with addition.
  2. 2. With 4 shapes and 3 renderers, how many classes do you need with a bridge (excluding the two base types)?

    A bridge needs one class per shape plus one per renderer: 4 + 3 = 7. Pure inheritance would need 4 x 3 = 12.
  3. 3. What is the 'bridge' itself in the pattern?

    The abstraction holds a field of the implementor type and forwards low-level work to it.
  4. 4. How does Bridge differ from Adapter?

    Intent is the difference: Bridge is a design decision for independent variation, Adapter is a retrofit for compatibility.
  5. 5. Which situation signals that your code should be refactored toward a Bridge?

    Names that combine two orthogonal attributes show a class explosion: shape and color should be separate hierarchies connected by composition.

Summary

  • Bridge splits an abstraction (what the client uses) from its implementation (the low-level work) and connects them by composition.
  • It turns a product of classes (shapes x renderers) into a sum (shapes + renderers).
  • Because the abstraction holds its implementor in a field, the implementation can be swapped at run time.
  • JDBC, UI toolkits and logging frameworks are real bridges.
  • Bridge is planned up front for two axes of change; Adapter is a retrofit; Strategy swaps one algorithm in one class.
  • Add a bridge when a second axis of variation really appears, not before.

Next lesson: Composite.