Bridge

Split something that varies in two ways into two separate class hierarchies, joined by a reference, so each side can grow on its own.

Description

When a concept varies along two independent dimensions, subclassing multiplies: Sales-HTML, Sales-CSV, Stock-HTML, Stock-CSV… The bridge breaks that into two hierarchies. The abstraction (reports: what to show) holds a reference to an implementor (renderers: how to output it).

Now a new report works with every renderer, and a new renderer works with every report. Two reports and two renderers take four classes instead of four combinations, and the gap widens with every addition.

Class diagram

bridge«abstract»Report# renderer: Renderer+ render(): string«interface»Renderer+ title(text)+ table(rows)SalesReport+ render()StockReport+ render()HtmlRenderer+ title(text)+ table(rows)CsvRenderer+ title(text)+ table(rows)

Bridge pattern class diagram

  1. Abstract class Report with + render(): string.
  2. Interface Renderer with + title(text), + table(rows).
  3. Class SalesReport with + render().
  4. Class StockReport with + render().
  5. Class HtmlRenderer with + title(text), + table(rows).
  6. Class CsvRenderer with + title(text), + table(rows).
  7. Report bridge Renderer.
  8. SalesReport extends Report.
  9. StockReport extends Report.
  10. HtmlRenderer implements Renderer.
  11. CsvRenderer implements Renderer.

Sequence diagram

AppSalesReportHtmlRenderer1. new SalesReport(new HtmlRenderer())2. render()3. title("Sales by quarter")4. <h1>…</h1>5. table(rows)6. <table>…</table>7. HTML report

Bridge pattern sequence diagram

  1. App → SalesReport: new SalesReport(new HtmlRenderer()).
  2. App → SalesReport: render().
  3. SalesReport → HtmlRenderer: title("Sales by quarter").
  4. SalesReport receives "<h1>…</h1>" back from HtmlRenderer.
  5. SalesReport → HtmlRenderer: table(rows).
  6. SalesReport receives "<table>…</table>" back from HtmlRenderer.
  7. App receives "HTML report" back from SalesReport.

TypeScript example

Reports decide what to show; renderers decide the output format. Any report works with any renderer, so two of each give four combinations from four classes.

bridge.ts
// Bridge: reports (what to show) and renderers (how to output it) vary independently.

interface Row {
  label: string;
  value: number;
}

// Implementor: the "how" side
interface Renderer {
  title(text: string): string;
  table(rows: Row[]): string;
}

class HtmlRenderer implements Renderer {
  title(text: string): string {
    return `<h1>${text}</h1>`;
  }
  table(rows: Row[]): string {
    return `<table>${rows.map((r) => `<tr><td>${r.label}</td><td>${r.value}</td></tr>`).join("")}</table>`;
  }
}

class CsvRenderer implements Renderer {
  title(text: string): string {
    return `# ${text}`;
  }
  table(rows: Row[]): string {
    return ["label,value", ...rows.map((r) => `${r.label},${r.value}`)].join("\n");
  }
}

// Abstraction: the "what" side, holding a reference across the bridge
abstract class Report {
  protected readonly renderer: Renderer;
  constructor(renderer: Renderer) {
    this.renderer = renderer;
  }
  abstract render(): string;
}

// Refined abstractions
class SalesReport extends Report {
  render(): string {
    const rows = [
      { label: "Q1", value: 120 },
      { label: "Q2", value: 180 },
    ];
    return `${this.renderer.title("Sales by quarter")}\n${this.renderer.table(rows)}`;
  }
}

class StockReport extends Report {
  render(): string {
    const low = [{ label: "mug", value: 3 }];
    return `${this.renderer.title("Low stock")}\n${this.renderer.table(low)}`;
  }
}

export function demo(): string[] {
  // 2 reports × 2 renderers = 4 combinations, from 4 classes, not 4 subclasses each
  return [new SalesReport(new HtmlRenderer()).render(), new StockReport(new CsvRenderer()).render()];
}

When to use it

Use it when

  • A class is growing subclasses for every combination of two concerns, such as shape × rendering engine or message × delivery channel.
  • You want to choose or switch the implementation at runtime.
  • Platform-specific code (database driver, OS, rendering backend) should stay out of the high-level logic.

Avoid it when

  • There's only one dimension of variation. That's plain inheritance or Strategy.
  • There's one implementation and no realistic second. The split is speculative.
  • The two sides are tightly coupled anyway. The bridge will just add indirection.

Pitfalls

  • Drawing the split in the wrong place. If every new report needs a new renderer method, the implementor interface is too specific.
  • An implementor interface that grows to cover every abstraction's needs. Keep it to primitive operations the abstraction combines.
  • Confusing it with Adapter: a bridge is designed up front so two sides can vary; an adapter is retrofitted to make existing pieces fit.

Alternatives

Strategy

Choose it when only one dimension varies and you want to swap an algorithm.

Adapter

Choose it when you're making an existing, incompatible class fit after the fact.

Abstract Factory

Choose it when you need to create the matching implementor objects for a whole platform.