100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Bridge Pattern?

Learn the Bridge design pattern — decoupling abstraction from implementation to avoid class explosion — with a Java example.

hardQ30 of 226 in Object Oriented Programming Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

The Bridge pattern is a structural design pattern that decouples an abstraction from its implementation by putting them in two separate class hierarchies connected through composition, so each hierarchy can vary independently.

Without Bridge, combining N abstraction variants with M implementation variants through inheritance alone produces an N-by-M explosion of subclasses (e.g. RedCircle, BlueCircle, RedSquare, BlueSquare). Bridge instead gives the abstraction a reference to an implementation interface, so the abstraction hierarchy only grows with new abstraction variants, and the implementation hierarchy only grows with new implementation variants, and any combination is assembled at runtime by composing the two. This is the same 'favor composition over inheritance' principle applied specifically to prevent class-hierarchy explosion when two dimensions of variation exist.

  • Avoids the combinatorial class explosion of a single deep hierarchy
  • Abstraction and implementation can evolve and be extended independently
  • Implementation can be swapped at runtime, not just compile time
  • Keeps client code decoupled from concrete implementation details

AI Mentor Explanation

A team separates "match format" (Test, ODI, T20) from "venue type" (day, day-night, indoor) instead of creating a separate rulebook for every format-venue combination. A Test match at a day-night venue simply composes the Test-format rules with the day-night venue rules, rather than needing its own dedicated rulebook. That is the Bridge pattern: two independent dimensions are connected through composition instead of being multiplied into one bloated hierarchy.

Step-by-Step Explanation

  1. Step 1

    Identify two independent dimensions

    Recognize an abstraction hierarchy and an implementation hierarchy that vary independently.

  2. Step 2

    Define an implementation interface

    Create an interface representing the low-level operations, with concrete implementers.

  3. Step 3

    Give the abstraction a reference to it

    The abstraction class holds an implementation-interface field, set via composition.

  4. Step 4

    Delegate and extend independently

    Abstraction subclasses add high-level behavior; implementer subclasses add low-level variants, both growing separately.

What Interviewer Expects

  • Understanding of the N-by-M class explosion problem Bridge solves
  • Clear separation of abstraction hierarchy vs implementation hierarchy
  • Recognition that composition, not inheritance, links the two
  • Ability to distinguish Bridge from Adapter (Bridge is designed upfront; Adapter retrofits incompatible interfaces)

Common Mistakes

  • Confusing Bridge with Adapter (Adapter fixes existing incompatibility; Bridge is a proactive design decision)
  • Trying to solve the two-dimension problem with a single deep inheritance tree
  • Not recognizing when two independent dimensions of variation actually exist
  • Coupling the abstraction directly to a concrete implementer instead of the interface

Best Answer (HR Friendly)

The Bridge pattern splits an abstraction and its implementation into two separate class hierarchies connected by composition, so you can mix and match combinations without creating a new subclass for every possible pairing. It is the fix for the class explosion you get when two independent things need to vary at once.

Code Example

Bridge decoupling shape from rendering implementation
// Implementation hierarchy
interface Renderer {
    void renderCircle(float radius);
}

class VectorRenderer implements Renderer {
    public void renderCircle(float radius) {
        System.out.println("Drawing circle as vectors, radius " + radius);
    }
}

class RasterRenderer implements Renderer {
    public void renderCircle(float radius) {
        System.out.println("Drawing circle as pixels, radius " + radius);
    }
}

// Abstraction hierarchy, bridged to Renderer via composition
abstract class Shape {
    protected Renderer renderer;
    Shape(Renderer renderer) { this.renderer = renderer; }
    abstract void draw();
}

class Circle extends Shape {
    private float radius;
    Circle(Renderer renderer, float radius) {
        super(renderer);
        this.radius = radius;
    }
    void draw() { renderer.renderCircle(radius); }
}

Shape circle = new Circle(new VectorRenderer(), 5); // any shape + any renderer
circle.draw();

Follow-up Questions

  • How does the Bridge pattern differ from the Adapter pattern?
  • What is the class explosion problem, and how does Bridge specifically prevent it?
  • Can you give an example of the JDBC driver architecture as a real-world Bridge?
  • When would Bridge be overkill compared to a simpler design?

MCQ Practice

1. The Bridge pattern primarily prevents?

Bridge avoids an N-by-M subclass explosion by splitting abstraction and implementation into two hierarchies joined by composition.

2. How is the abstraction connected to its implementation in the Bridge pattern?

The abstraction holds a reference to an implementation interface, connecting the two hierarchies via composition.

3. Bridge differs from Adapter mainly because?

Bridge is a proactive structural decision made during design, while Adapter is typically applied reactively to reconcile mismatched interfaces.

Flash Cards

Bridge pattern in one line?Decouples an abstraction from its implementation via composition, so both vary independently.

Problem it solves?N-by-M class explosion from combining two independent dimensions through inheritance alone.

How are the two hierarchies linked?The abstraction holds a composed reference to an implementation interface.

Bridge vs Adapter?Bridge is designed upfront for independent variation; Adapter retrofits incompatible interfaces after the fact.

1 / 4

Continue Learning