Bridge
Intent
Split a control hierarchy from the device hierarchy it drives, so remotes and devices can each grow without forcing a new class for every combination.
Motivation
A shop sells two device brands — a Sony TV and an LG speaker — and two remotes — a basic remote and a mute remote. Encoding every pair as its own class (basic-remote-for-Sony, mute-remote-for-LG, and so on) means each new brand multiplies the remote code. The bridge keeps one remote side that delegates to a device contract, so adding a brand means writing one device, and adding a remote feature means writing one remote.
Structure
flowchart LR Basic[Basic Remote] --> Device Mute[Mute Remote] --> Device Device --> Sony[Sony TV] Device --> LG[LG Speaker]
- Abstraction (Basic Remote) — owns the power flow; delegates brand-specific steps to the device.
- Refined abstraction (Mute Remote) — extends the remote with a mute step, still delegating.
- Implementor (Device) — the shared brand contract (
turn_on,mute). - Concrete implementors — Sony TV and LG Speaker with brand-specific behavior.
Consequences
- Benefit: remotes and brands vary independently — one new unit per addition instead of a combination explosion.
- Cost / trade-off: an extra delegation layer; simple one-remote-one-device setups gain indirection they do not need.
Basic Recap
Bridge separates an abstraction’s user-facing behavior from the implementation mechanism it delegates to. The two sides can vary independently, avoiding a subclass for every combination of feature and platform. An abstraction defines the workflow, while an implementor supplies the primitive operations. Unlike an adapter, a bridge is usually designed up front around two dimensions that are expected to evolve.
Complex Scenario
A signing platform supports single-document and batch-signing workflows, and it
can use either a local hardware security module or a cloud signing provider. A
SigningFlow abstraction handles documents, user consent, and completion rules;
a Signer implementor performs provider-specific signing primitives. Adding a
new workflow should not require provider subclasses, and adding a provider
should not duplicate consent policy. The bridge boundary uses portable signing
requests and results rather than vendor response objects.
Edge cases
- Retries: Repeating a batch after a timeout must carry stable request keys; the flow can resume completed items without assuming every provider call was rolled back.
- Partial failure: Some documents may be signed before another fails, so the abstraction needs item-level status and a policy for acceptable partial completion.
- Concurrency: A flow object should not switch its signer mid-operation; concurrent jobs may use separate bridge instances with isolated credentials.
Keep orchestration above the bridge and primitive capabilities below it. Moving vendor-specific batch workflow into both sides erodes the independence.
Sketch (illustrative pseudocode, not runnable):
BatchSigningFlow.execute(documents, consent):
verifyConsent(consent)
results = ResultSet()
for document in documents:
request = portableRequest(document, consent.batchKey)
prior = results.completed(request.key)
if prior exists:
continue
outcome = signer.signOrResume(request)
results.record(document.id, outcome)
return summarize(results)
SingleSigningFlow.execute(document, consent):
verifyConsent(consent)
return signer.signOrResume(portableRequest(document, consent.key))
When to Use / When Not to Use
Use when:
- Two dimensions (controls × devices, views × renderers) would otherwise multiply into combined classes.
- Either side is expected to grow on its own release cycle.
Avoid when:
- Only one remote and one device exist with no growth foreseen — direct calls are simpler.
- The two sides never change independently; the split buys nothing.
Examples
Links to runnable code (same scenario, four languages):
examples/elixir/bridge/examples/dart/bridge/examples/go/bridge/examples/rust/bridge/
Expected outcome: a basic remote powers on the Sony TV and a mute remote silences the LG speaker.
Anti-pattern / Failure Mode
-
Misuse: Splitting one dimension that never varies into two hierarchies.
-
Review signal: Abstraction and implementation changing in lockstep anyway.
-
Misuse symptom: Every abstraction method is a one-line pass-through to an identical implementor method, and the two hierarchies always change together. The bridge then doubles types without separating genuine axes of variation.
-
Review signal: Watch for parallel class names added in lockstep, vendor types crossing the implementor interface, and conditionals in the abstraction that select provider behavior. Tests should combine each flow with each signer through contracts rather than requiring a bespoke test class for every pair.
Related
- Adapter — grow both sides independently vs fit one side to the other.
- Strategy — vary implementation of one step vs vary both dimensions.
- Composition over Inheritance — the principle behind the split.
Exercise
Add a Philips TV device (no remote edits) and a sleep-timer remote that wraps any device. Both new units must work with the existing opposite side untouched.
Self-check
- What does the remote hold?
Answer
A reference to the device contract, not a concrete brand — it delegates brand steps. - Bridge vs Adapter: what differs?
Answer
Bridge is designed up front to separate two growing dimensions; Adapter retrofits one fixed mismatch. - When does Bridge pay off?
Answer
When either dimension grows — each addition costs one unit, not a full cross-product.