Skip to Content
Mengty Learns
Design

Bridge Pattern

Separate an abstraction from its implementation so both can evolve independently.

explanationintermediateTested with typescript 7.0.2Updated Nov 26, 2026

1. Intent

TL;DR

Split a large class or tightly coupled hierarchy into two independent hierarchies—Abstraction and Implementation—connected by composition instead of inheritance, so each side can vary without multiplying subclasses.

2. Problem

Suppose you are building an order-export module. You start with an OrderExporter base class and subclass it per backend: PostgresOrderExporter and CsvOrderExporter. Next, you need an AsyncOrderExporter that batches writes and retries transient errors. Suddenly you need AsyncPostgresOrderExporter and AsyncCsvOrderExporter.

Every new exporter mode or storage backend multiplies the class count (M exporters × N drivers). Adding a third storage driver (say S3) means writing two more exporter subclasses; adding a third exporter mode means writing three more.

Inheritance couples both dimensions: M exporters × N drivers produces M×N subclasses.

3. Solution

Instead of inheriting from every combination, extract the storage operations into an interface (StorageDriver) and give OrderExporter a reference to a StorageDriver instance.

  • The Abstraction (OrderExporter, AsyncExporter) handles export orchestration like formatting payloads, batching, or retrying.
  • The Implementation (StorageDriver, implemented by PostgresDriver and CsvDriver) handles low-level I/O like connect(), writeRecord(), and close().

The reference from OrderExporter to StorageDriver is the bridge. Now you can add new exporters or new storage drivers independently (M + N classes instead of M × N).

4. Real-world analogy

5. Flow: watch it work

Bridge Pattern: OrderExporter → StorageDriver

Overview of Bridge Pattern: OrderExporter → StorageDriver. 8 steps.
Overview8 steps in this flow

Press Start or tap any step dot to trace the path.

▸All steps (8)

6. Structure

Composition over inheritance: Abstraction delegates to the Implementor interface.

7. Code

// Implementation interface
interface StorageDriver {
  connect(): Promise<void>;
  writeRecord(collection: string, record: Record<string, unknown>): Promise<string>;
  close(): Promise<void>;
}

// Concrete Implementations
class PostgresDriver implements StorageDriver {
  async connect() { /* open connection pool */ }
  async writeRecord(collection: string, record: Record<string, unknown>) {
    return `pg:${collection}:${String(record.id ?? 'new')}`;
  }
  async close() { /* drain pool */ }
}

class CsvDriver implements StorageDriver {
  private rows: string[] = [];
  async connect() { this.rows = []; }
  async writeRecord(_collection: string, record: Record<string, unknown>) {
    this.rows.push(JSON.stringify(record));
    return `csv:row-${this.rows.length}`;
  }
  async close() { /* flush file handle */ }
}

// Abstraction
class OrderExporter {
  constructor(protected readonly driver: StorageDriver) {}

  async exportOrder(order: { id: string; totalCents: number }): Promise<string> {
    await this.driver.connect();
    const receipt = await this.driver.writeRecord('orders', order);
    await this.driver.close();
    return receipt;
  }
}

// Refined Abstraction
class AsyncExporter extends OrderExporter {
  async exportBatch(orders: { id: string; totalCents: number }[]): Promise<string[]> {
    await this.driver.connect();
    const receipts = await Promise.all(
      orders.map((order) => this.driver.writeRecord('orders', order)),
    );
    await this.driver.close();
    return receipts;
  }
}

// Usage
const pgExporter = new OrderExporter(new PostgresDriver());
await pgExporter.exportOrder({ id: 'ord_1', totalCents: 2500 });

const csvBatch = new AsyncExporter(new CsvDriver());
await csvBatch.exportBatch([
  { id: 'ord_2', totalCents: 1200 },
  { id: 'ord_3', totalCents: 4900 },
]);
Run it: labs/bridge-pattern-ts

Run OrderExporter and AsyncExporter against PostgresDriver and CsvDriver, and extend both hierarchies independently.

cd labs/bridge-pattern-ts && npm install && npm run typecheck && npm test && npm start

8. When to use / when to avoid

When to use
  • You have two orthogonal dimensions that vary independently (e.g. UI controls × platform APIs, or shapes × renderers).
  • You want to switch implementations at runtime without changing client code.
  • A monolithic class is growing too large because it mixes high-level logic with platform-specific details.
When to avoid
  • There is only one implementation and no realistic plan to add another.
  • The abstraction and implementation are tightly bound by performance or lifecycle constraints.
  • A simple function callback or Strategy would solve the variation with less boilerplate.

9. How to implement

Identify the two independent dimensions

Separate what the caller interacts with (Abstraction) from the low-level work (Implementation).

Define the Implementor interface

Keep it focused on primitive operations (connect, writeRecord, close) rather than high-level workflows.

Store the Implementor on the Abstraction

Inject the StorageDriver via the OrderExporter constructor and delegate work to it.

Extend either side independently

Add AsyncExporter without touching PostgresDriver or CsvDriver, and add S3Driver without touching any exporter class.

10. Pros / Cons

Pros
  • Prevents M × N subclass explosion when two dimensions grow.
  • Follows Open/Closed Principle: add new abstractions or implementations without modifying existing ones.
  • Keeps platform-specific details isolated behind a clean interface.
Cons
  • Adds extra indirection and interfaces, which is overkill for simple hierarchies.
  • Harder to read at a glance if both sides are over-abstracted too early.

11. Relations

Bridge vs Adapter

Bridge is designed up front so two hierarchies evolve independently; Adapter wraps an incompatible existing interface after the fact.

Bridge vs Strategy

Both use composition, but Strategy swaps a single interchangeable algorithm while Bridge decouples an entire abstraction hierarchy from an implementation hierarchy.

Abstract Factory + Bridge

An Abstract Factory can create and pair the right Abstraction and Implementation together at startup.

12. Check yourself

Check Yourself

1. You have 3 Abstraction variants and 4 Implementation backends. Roughly how many concrete classes do you need with Bridge vs inheritance?

2. Which OOP mechanism forms the 'bridge' between Abstraction and Implementation?

Sources