AlgoViz

Bridge pattern

medium 50 min

The Bridge pattern splits one exploding hierarchy into two that are joined by a reference, so that what is wanted and how a platform does it can grow apart.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:19

See it run

Two families, one reference. Watch what moves when you change one side.

Operations

Shapes and renderers

One circle, two graphics APIs.

Step 1 of 7

Implementors

clientGamecircleCircleradius7x, y5, 10open_glOpenGlRendererdirect_xDirectXRenderer

Two renderers exist. They know nothing about shapes.

Step 1: Implementors

class Shape(ABC):    def __init__(self, api, x, y):        self._api = api            # the bridge        self.x, self.y = x, y    def use(self, api):        self._api = apiclass Circle(Shape):    def draw(self):        return self._api.draw_circle(self.x, self.y, self.radius)    def resize(self, factor):        self.radius *= factorclass OpenGlRenderer(DrawingApi):    def draw_circle(self, x, y, radius):        return "OpenGL circle"class DirectXRenderer(DrawingApi):    def draw_circle(self, x, y, radius):        return "DirectX circle"
Watch
The api arrow
Result
Same circle, other API

The arrow moves once. The circle keeps its radius.

In the real world

The same bridge pattern doing real jobs. Pick one and step through it.

Both suites are green, the pair is red.

Step 1 of 3

Set 35

remoteFadeRemotewants35radioRadiostep3set_volume(35)

The remote wants 35 and waits until the device says 35.

Step 1: Set 35

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Steps named after the abstractions

    critical

    Every new shape edits every renderer. M times N is back.

    Wrong
    class Api(ABC):    def render_triangle(self, points): ...    def render_star(self, points): ...
    Right
    class Canvas(ABC):    def fill_polygon(self, points): ...
  • The right side knows the left

    major

    The arrow goes back over the bridge.

    Wrong
    def draw(self, shape):    if isinstance(shape, Circle): ...
    Right
    def fill_circle(self, x, y, r): ...
  • Creating the implementor inside

    major

    The choice is fixed again, and tests need a real platform.

    Wrong
    class Circle(Shape):    def __init__(self):        self._api = OpenGlRenderer()
    Right
    class Circle(Shape):    def __init__(self, api):        self._api = api
  • Keeping a handle of the platform

    critical

    After a swap the number means something else.

    Wrong
    self.texture = api.upload("fox.png")
    Right
    self.source = "fox.png"
  • A bridge with one side

    minor

    One implementation for years: a toll without a river.

    Wrong
    class ClockImpl(ABC): ...class OnlyClock(ClockImpl): ...
    Right
    class Clock: ...       # split when a second one comes
  • Repairing one pair

    major

    The abstraction now knows a platform.

    Wrong
    if isinstance(self._device, Radio):    percent = round(percent, -1)
    Right
    # contract: a device may round by 5

Interview tips

  • Find the two questions

    • Two nouns in a name.
    • Two reasons to change.
    • Does every pair make sense?
  • Design the middle

    • Small steps.
    • Words of the platform.
    • Units, origin, errors.
  • One direction

    • Left holds right.
    • Numbers cross, objects do not.
    • Features keep their state left.
  • Grow with care

    • New variant: one class.
    • New step: with a default.
    • Third dimension: data first.
  • Test apart

    • Left with a strict fake.
    • Right with a contract.
    • A few pairs.