Bridge pattern
medium 50 minThe 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
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
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
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
criticalEvery new shape edits every renderer. M times N is back.
Wrongclass Api(ABC): def render_triangle(self, points): ... def render_star(self, points): ...Rightclass Canvas(ABC): def fill_polygon(self, points): ...The right side knows the left
majorThe arrow goes back over the bridge.
Wrongdef draw(self, shape): if isinstance(shape, Circle): ...Rightdef fill_circle(self, x, y, r): ...Creating the implementor inside
majorThe choice is fixed again, and tests need a real platform.
Wrongclass Circle(Shape): def __init__(self): self._api = OpenGlRenderer()Rightclass Circle(Shape): def __init__(self, api): self._api = apiKeeping a handle of the platform
criticalAfter a swap the number means something else.
Wrongself.texture = api.upload("fox.png")Rightself.source = "fox.png"A bridge with one side
minorOne implementation for years: a toll without a river.
Wrongclass ClockImpl(ABC): ...class OnlyClock(ClockImpl): ...Rightclass Clock: ... # split when a second one comesRepairing one pair
majorThe abstraction now knows a platform.
Wrongif 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.