Command Palette

Search for a command to run...

PHASE 2Beginner ~7 min· topic 11 of 22Structural

Structural 6 — Bridge

In one line

Decouple an abstraction from its implementation so both can vary independently — avoids the 2D class explosion.

0/22 · 0%

Think of it like this

A TV remote and TVs. Any remote (basic or smart) can work with any TV brand (Sony or LG). You can make a new remote without making new TVs, and vice versa.

Key ideas

  1. 01

    Solves: shapes × rendering engines (Circle×Raster, Circle×Vector, Square×Raster...) — the matrix problem.

  2. 02

    Abstraction holds a reference to an Implementation (renderer), instead of inheriting it.

  3. 03

    Similar to Strategy in shape; Bridge is more structural, Strategy behavioral.

Java / Spring map

  • →

    JDBC (the DriverManager/Connection API is the abstraction, vendor drivers are the implementations); SLF4J over Logback/Log4j.

Code & diagrams

Bridge.javajava

Message urgency × delivery channel: M + N classes instead of M × N.

// Implementation hierarchy: HOW it is delivered
public interface Channel { void deliver(String to, String body); }
public final class EmailChannel implements Channel { public void deliver(String to, String b) { /* SMTP */ } }
public final class SmsChannel   implements Channel { public void deliver(String to, String b) { /* SMS */ } }

// Abstraction hierarchy: WHAT kind of message, holding a bridge to a Channel
public abstract class Notification {
  protected final Channel channel;                 // the bridge
  protected Notification(Channel c) { this.channel = c; }
  public abstract void send(String to, String msg);
}

public final class NormalNotification extends Notification {
  public NormalNotification(Channel c) { super(c); }
  public void send(String to, String msg) { channel.deliver(to, msg); }
}

public final class UrgentNotification extends Notification {
  public UrgentNotification(Channel c) { super(c); }
  public void send(String to, String msg) {
    for (int i = 0; i < 3; i++) channel.deliver(to, "[URGENT] " + msg);  // escalation policy
  }
}

// 2 kinds × 2 channels = 4 combos from 4 classes; adding Push adds ONE class.
// new UrgentNotification(new SmsChannel()).send("+91...", "server down");

Explain without notes

01

Bridge vs Adapter vs Strategy in one breath.

Practice

01

Model Notification (email/SMS) × urgency (normal/urgent) with Bridge.

Trade-offs

  • ↔

    Only pay for Bridge when BOTH dimensions actually vary.

Completion checklist

  • I can spot an M × N class explosion and refactor it into two hierarchies joined by a bridge.

Back to phase