design patterns ·

Bridge

Decouples an abstraction from its implementation so both can vary independently.

medium1hgeneraljava
Ask GPTConfidence

Theory

Explanation

Intuition first, formal definition second. Skim the bullets if you already know this; read the prose if you don't.

When you have two orthogonal dimensions of variation (e.g., shapes and rendering engines), using inheritance to cover every combination creates a class explosion. Bridge separates them into two independent hierarchies connected by composition.

The Abstraction holds a reference to an Implementor. The Abstraction delegates implementation-specific work to the Implementor. Both hierarchies can extend independently without touching each other.

When to use

When you want to avoid a cartesian subclass explosion - e.g., shapes × rendering engines, devices × remote controls, platforms × UI themes.

When not to

When there is only one implementation or the two dimensions do not vary independently.

Key insights

  • Bridge uses composition where inheritance would create O(n×m) subclasses
  • The Implementor interface does not need to match the Abstraction interface
  • Strategy pattern is similar but focuses on algorithms; Bridge focuses on structure
  • Often used in cross-platform frameworks to separate UI logic from platform APIs