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 pattern class diagram
- Abstract class Report with + render(): string.
- Interface Renderer with + title(text), + table(rows).
- Class SalesReport with + render().
- Class StockReport with + render().
- Class HtmlRenderer with + title(text), + table(rows).
- Class CsvRenderer with + title(text), + table(rows).
- Report bridge Renderer.
- SalesReport extends Report.
- StockReport extends Report.
- HtmlRenderer implements Renderer.
- CsvRenderer implements Renderer.
Sequence diagram
Bridge pattern sequence diagram
- App → SalesReport: new SalesReport(new HtmlRenderer()).
- App → SalesReport: render().
- SalesReport → HtmlRenderer: title("Sales by quarter").
- SalesReport receives "<h1>…</h1>" back from HtmlRenderer.
- SalesReport → HtmlRenderer: table(rows).
- SalesReport receives "<table>…</table>" back from HtmlRenderer.
- 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: 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.