bigonotes
CS ConceptsDesign Patterns

Bridge Pattern

Split two independently varying dimensions into separate hierarchies joined by composition, turning N × M subclasses into N + M.

5 min readLast updated

When a class varies along two dimensions at once (shape and color, remote type and TV brand), inheritance makes you write a subclass for every combination. Bridge splits the two dimensions into separate hierarchies and connects them with a reference, so each side can grow on its own. You'll spot it any time a class name starts to read like a pair, such as RedCircle or SonyBasicRemote.

Intent#

Decouple an abstraction from its implementation so that the two can vary independently. (GoF)

Think of TV remotes. The abstraction is the remote (basic, advanced); the implementation is the TV (Sony, Samsung, LG). A remote holds a reference to a TV instead of inheriting from one, so any remote works with any TV. No SonyBasicRemote, SamsungAdvancedRemote, and so on.

So the key idea is to connect two hierarchies with composition, not inheritance.

The problem: class explosion#

To see why that matters, look at shapes and colors. With inheritance alone, every shape needs a subclass for every color:

# Two dimensions: shape (Circle, Square) and color (Red, Blue, Green)
class Circle: ...
class Square: ...

class RedCircle(Circle): ...
class BlueCircle(Circle): ...
class GreenCircle(Circle): ...
class RedSquare(Square): ...
class BlueSquare(Square): ...
class GreenSquare(Square): ...
# 2 shapes × 3 colors = 6 classes; 5 shapes × 10 colors = 50.
# A new shape needs a class per color; a new color needs a class per shape.

Bridge pulls color out into its own hierarchy and gives each shape a reference to a color. Now the two sides grow separately:

from abc import ABC, abstractmethod


class Color(ABC):  # implementation hierarchy
    @abstractmethod
    def fill(self) -> str: ...


class Red(Color):
    def fill(self) -> str:
        return "red"


class Blue(Color):
    def fill(self) -> str:
        return "blue"


class Shape(ABC):  # abstraction hierarchy
    def __init__(self, color: Color):
        self.color = color  # the bridge

    @abstractmethod
    def draw(self) -> None: ...


class Circle(Shape):
    def draw(self) -> None:
        print(f"Circle in {self.color.fill()}")


class Square(Shape):
    def draw(self) -> None:
        print(f"Square in {self.color.fill()}")


Circle(Red()).draw()
Square(Blue()).draw()
# 2 shapes + 2 colors = 4 classes. A new shape or color is one class.

Structure#

The shapes example showed the idea in small. Here are the formal roles, named after the remote-and-TV analogy: Bridge splits one class hierarchy into two that are joined by a reference.

Bridge: a Remote hierarchy and a Device hierarchy joined by a single has-a reference
Participant Role
Abstraction High-level interface the client uses; holds a reference to an Implementor (Remote)
Refined abstraction Variants of the abstraction (BasicRemote, AdvancedRemote)
Implementor Interface for the low-level side (Device)
Concrete implementor Platform- or vendor-specific code (SonyTV, SamsungTV)

Example: remote + device#

Here's the remote analogy as code. Each remote holds a Device and only calls its primitive operations:

from abc import ABC, abstractmethod


# Implementor
class Device(ABC):
    @abstractmethod
    def turn_on(self) -> None: ...

    @abstractmethod
    def turn_off(self) -> None: ...


# Concrete implementors
class SonyTV(Device):
    def turn_on(self) -> None:
        print("Sony TV: ON")

    def turn_off(self) -> None:
        print("Sony TV: OFF")


class SamsungTV(Device):
    def turn_on(self) -> None:
        print("Samsung TV: ON")

    def turn_off(self) -> None:
        print("Samsung TV: OFF")


# Abstraction: holds the bridge reference
class Remote(ABC):
    def __init__(self, device: Device):
        self.device = device

    @abstractmethod
    def operate(self) -> None: ...


# Refined abstractions
class BasicRemote(Remote):
    def operate(self) -> None:
        self.device.turn_on()
        self.device.turn_off()


class AdvancedRemote(Remote):
    def operate(self) -> None:
        self.device.turn_on()
        print("  (advanced features)")
        self.device.turn_off()


# Any remote with any device
BasicRemote(SonyTV()).operate()
AdvancedRemote(SamsungTV()).operate()

Running it prints:

Sony TV: ON
Sony TV: OFF
Samsung TV: ON
  (advanced features)
Samsung TV: OFF

Real-world examples#

Bridge shows up wherever an API and its backends change separately:

  • Database APIs: your code talks to a generic connection and cursor; vendor drivers (MySQL, PostgreSQL, Oracle) supply the implementation.
  • GUI toolkits: widgets are the abstraction; OS-specific backend classes are the implementation.
  • Logging: a logger API plus pluggable file/console/network appenders.
  • Business systems:
    • notification type × channel (email/SMS/Slack)
    • report × format (PDF/Excel/HTML)
    • file operations × storage backend (local/cloud)
    • shapes × renderer (vector/raster)

When to use it#

Bridge earns its extra interfaces when:

  • Two dimensions vary independently and both will grow.
  • Inheritance would multiply classes (N × M).
  • You want to switch implementations at runtime or support several platforms.
  • Several abstractions should share one implementation.

When not to use it#

The extra interfaces cost you something, so skip Bridge when:

  • Only one dimension varies. Use Strategy or plain polymorphism.
  • The system is tiny (two or three classes).
  • The abstraction and implementation always change together, or the abstraction needs intimate implementation details.

Pitfalls#

Even when two dimensions are real, watch for these traps:

  • Designing it too early. Bridge pays off when you're sure of two dimensions; guessing adds interfaces nobody needs.
  • Leaky implementor interfaces. Keep Device to primitive operations so the abstraction side can combine them freely.
  • Indirection. One more hop for every call and one more thing to trace when debugging.

Bridge vs similar patterns#

The main difference from its neighbors is how many hierarchies vary:

Pattern How many hierarchies vary When it's applied
Strategy One (the algorithm family; the context is fixed) Any time
Bridge Two (abstraction and implementation both have subclasses) Designed up front
Adapter n/a: fixes one mismatched interface After the fact

Strategy is the one people mix up most, because both hold a reference to another object. The difference is whether the holder has subclasses too:

from __future__ import annotations


# Strategy: only the strategy varies
class Sorter:
    def __init__(self, strategy: SortStrategy):
        self.strategy = strategy

    def sort(self) -> None:
        self.strategy.sort()


# Bridge: Remote has subclasses AND Device has subclasses
class Remote:
    def __init__(self, device: Device):
        self.device = device

    def operate(self) -> None:
        self.device.turn_on()

Two more neighbors are easy to confuse with Bridge:

  • vs Abstract Factory: Abstract Factory creates families of objects; Bridge composes two hierarchies at runtime. They're often used together: a factory picks which implementor to plug in.
  • vs State: State changes behavior as an object's state changes; Bridge separates what from how.

Key takeaways#

  • Bridge = two hierarchies (abstraction and implementation) joined by a has-a reference.
  • It turns N × M subclasses into N + M classes.
  • "Prefer composition over inheritance" when two things vary orthogonally.
  • One varying dimension → Strategy. Two → Bridge. Fixing an existing mismatch → Adapter.
Revision notes · 2 min

Core idea#

Bridge splits a class that varies along two dimensions into two hierarchies. A has-a reference joins them, so each side grows on its own. N × M subclasses become N + M classes.

Key points#

  • Without Bridge, every shape needs a subclass per color: 5 shapes × 10 colors = 50 classes.
  • The abstraction holds a reference to an implementor. That reference is the bridge.
  • Join the hierarchies with composition, not inheritance.
  • Keep the implementor interface to primitive operations. The abstraction side combines them.
  • You can switch implementations at runtime. Several abstractions can share one implementation.
  • Examples: database API and vendor drivers, GUI widgets and OS backends, report × format, notification × channel.

Participants#

Participant Role
Abstraction Interface the client uses; holds an implementor (Remote)
Refined abstraction Variants (BasicRemote, AdvancedRemote)
Implementor Interface for the low-level side (Device)
Concrete implementor Vendor-specific code (SonyTV, SamsungTV)

Bridge vs similar patterns#

Pattern What varies When applied
Strategy One hierarchy (the algorithm); the context is fixed Any time
Bridge Two hierarchies, both with subclasses Designed up front
Adapter Fixes one mismatched interface After the fact
Abstract Factory Creates families; often picks the implementor for Bridge With Bridge

Pitfalls#

  • Designing it too early. Use it only when you are sure of two dimensions.
  • Leaky implementor interfaces that expose more than primitives.
  • One more hop for each call, and one more layer to trace.
  • Skip it when only one dimension varies, the system is tiny, or both sides always change together.

Say it in an interview#

  • "Bridge is two hierarchies joined by a has-a reference. It turns N × M into N + M."
  • "One varying dimension is Strategy. Two is Bridge. Fixing an existing mismatch is Adapter."

Found a mistake? Email ask@bigonotes.com