RecapDifficulty: Advanced1 min

Recap: Bridge Pattern

Two hierarchies joined by one reference, so M plus N classes replace M times N.

Bridge splits a class that varies two ways into two hierarchies, joined by a reference from one to the other.

  • Intent. Decouple an abstraction from its implementation so that the two can vary independently.
  • Participants. The abstraction and its refined variants, the implementor interface and its concrete implementors, and the client.
  • The count. M variants times N variants as subclasses becomes M plus N classes, and a new variant on either side costs one class.
  • In the wild. JDBC: code written against java.sql interfaces, with a vendor driver behind them chosen by the URL.
  • The trap. A bridge with one implementor varies nothing; extract it when the second variant arrives.
  • Against Adapter and Strategy. An adapter is added after the fact; a strategy is one algorithm, where a bridge's abstraction has variants of its own.