STRUCTURAL PATTERNS / OBJECT DESIGN BRIEF

Bridge pattern

The Bridge pattern separates an abstraction from its implementation so both can vary independently.

AdvancedPhase 05 / Topic 2 of 7ResponsibilitiesCollaborationsExtensibility
01

Overview

The Bridge pattern separates an abstraction from its implementation so both can vary independently. Instead of a class hierarchy with every combination (EmailAlert, SmsAlert, UrgentEmailAlert, UrgentSmsAlert...), you have an abstraction hierarchy (Alert, UrgentAlert) that holds a reference to an implementation hierarchy (Channel: Email, SMS).

Bridge prevents the 'm x n' class explosion when two dimensions vary. It looks structurally like Strategy (composition with an interface), but the intent differs: Bridge is designed up front to decouple two orthogonal hierarchies that both evolve.

Remote controls and devices

A basic remote and an advanced remote (abstractions) can both control a TV or a radio (implementations). New remotes and new devices can be added independently because they communicate through a standard device interface.

02

When to use it

  • Two independent dimensions of variation (shape x renderer, message type x channel).
  • Implementations should be switchable at runtime.
  • You want to avoid a combinatorial subclass explosion.
03

Where it shows up in interviews

Two dimensions of change

Recognize it when: types x platforms, messages x channels.

  • Design a notification system with message types and channels
  • Design a drawing app with shapes and renderers
04

Where it is used in real software

JDBC

Your code uses the JDBC abstraction while vendor drivers are the implementation side.

SLF4J

Logging API (abstraction) bridged to Logback or Log4j (implementation).

Graphics APIs

UI toolkits separate widgets from rendering backends (OpenGL, Metal, Skia).

05

Key terms

Abstraction
High-level control layer (Alert, Shape).
Refined abstraction
Variants of the abstraction (UrgentAlert).
Implementor
Low-level interface (Channel, Renderer).
Concrete implementor
EmailChannel, SvgRenderer.
06

How it works, step by step

  1. 1
    Identify the two dimensions

    What varies independently?

  2. 2
    Define the implementor interface

    Primitive operations: send(to, text).

  3. 3
    Define the abstraction holding an implementor

    Alert(channel).

  4. 4
    Extend each side independently

    New alert types; new channels.

  5. 5
    Combine at runtime

    new UrgentAlert(new SmsChannel()).

07

Class count comparison

3 alert types x 4 channels

Step 1 / 2
DesignClassesAdding a 5th channel
Subclass per combination12+3 classes
Bridge3 + 4 = 7+1 class

NOWDesign: Subclass per combination | Classes: 12 | Adding a 5th channel: +3 classes

Bridge turns multiplication into addition.

08

Implementation

// Implementorinterface Channel { send(to: string, text: string): void }class EmailChannel implements Channel { send(to: string, t: string) { console.log("email", to, t); } }class SmsChannel implements Channel { send(to: string, t: string) { console.log("sms", to, t.slice(0, 160)); } } // Abstractionabstract class Alert {  constructor(protected channel: Channel) {}  abstract notify(to: string, message: string): void;} class InfoAlert extends Alert {  notify(to: string, message: string) { this.channel.send(to, message); }} class UrgentAlert extends Alert {  notify(to: string, message: string) {    this.channel.send(to, `URGENT: ${message}`);    this.channel.send(to, `Reminder: ${message}`); // urgent policy: send twice  }} new UrgentAlert(new SmsChannel()).notify("+15550100", "Server down");new InfoAlert(new EmailChannel()).notify("[email protected]", "Deploy complete");
09

Complexity and performance

Classes for m x nm + n

vs m x n.

Runtime cost1 delegation

Negligible.

10

Trade-offs

Upfront design

Bridge requires identifying the two dimensions early; applied too soon it adds indirection.

Implementor interface design

The implementor must expose primitives general enough for all abstractions.

11

Variants and related techniques

Bridge vs Strategy

Similar structure; Strategy swaps an algorithm, Bridge separates two evolving hierarchies.

Bridge vs Adapter

Adapter fixes incompatibility after the fact; Bridge is designed in advance.

12

Common mistakes

  • Using Bridge with only one dimension.

    Fix: A single interface (Strategy) is enough.

  • Leaking implementor details into abstractions.

    Fix: Keep implementors to primitive operations.

13

Interview questions

What problem does Bridge solve?

Class explosion when two dimensions vary independently. It splits them into two hierarchies connected by composition so each can grow without multiplying classes.

How is Bridge different from Strategy?

Structurally similar, but Bridge's intent is separating an abstraction hierarchy from an implementation hierarchy, both of which have subclasses, while Strategy swaps one algorithm inside a context.

14

Practice problems

ProblemDifficultyWhat it trains
Messages (info, urgent) x channels (email, SMS, push)MediumTwo hierarchies.
Shapes x renderers (SVG, canvas)MediumPrimitive operations.