Adapter pattern
easy 50 minThe Adapter pattern makes an existing class fit the interface that its client expects, without changing either of them.
Video lesson · Chapter 1 of 5
One client, two languages
See it run
Follow one call across the border: what goes in, what comes out, and what is translated.
Operations
Media player
Three formats, one call. The player never learns who plays.
Step 1 of 8
Wired
The player holds three objects of the same type. One is native, two are adapters.
Step 1: Wired
class AudioPlayer: def open(self, file_name, start=0): player = self._players.get(extension(file_name)) if player is None: raise PlaybackError("no player") return player.play(file_name, start)class Mp4Adapter(MediaPlayer): def play(self, file_name, start=0): code = self._decoder.load(file_name) if code != 0: raise PlaybackError(file_name) self._decoder.seek(start * 1000) return self._decoder.run()class VlcAdapter(MediaPlayer): def play(self, file_name, start=0): return self._engine.play_vlc(file_name, start * 1000)- Watch
- The middle column
- Result
- Units, order, errors
Read the two pills: seconds on the left side of an adapter, milliseconds on the right.
In the real world
The same adapter pattern doing real jobs. Pick one and step through it.
The same four tests for the fake and for the adapter.
Step 1 of 3
The fake
| Memory fake | First adapter | Fixed adapter | |
|---|---|---|---|
| Round trip | ok | ||
| Missing key | ok | ||
| a and A differ | ok | ||
| Empty text | ok |
The in-memory version keeps all four promises. Business tests use it.
Step 1: The fake
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Copying the vendor's interface
majorThe target is the vendor in disguise, so the next vendor does not fit.
Wrongclass SmsSender(ABC): def send_sms(self, number, raw, flags): ...Rightclass Notifier(ABC): def send(self, person, message): ...Ignoring the answer
criticalA refused file plays the previous one.
Wrongself._decoder.load(name)return self._decoder.run()Rightif self._decoder.load(name) != 0: raise PlaybackError(name)return self._decoder.run()Timeout reported as no
criticalThe money may have moved. The customer pays twice.
Wrongexcept TimeoutError: raise Declined(key)Rightexcept TimeoutError as e: raise OutcomeUnknown(key) from eA foreign type escapes
majorEvery caller now depends on the vendor's SDK.
Wrongdef charge(self, money, key): return self._sdk.create_charge(...)Rightdef charge(self, money, key): reply = self._sdk.create_charge(...) return Receipt("pennypay", reply["id"])Business rules inside
majorThe second vendor's adapter will not have the discount.
Wrongdef charge(self, money, key): if money.minor > 5000: money = with_discount(money)Rightdef charge(self, money, key): # translate only return self._send(money, key)One call per item
major100 network calls where one would do.
Wrongreturn [self._api.get(i) for i in ids]Rightreturn self._api.get_many(ids)
Interview tips
Start at the client
- Write the call you wish for.
- Name it in your words.
- Keep it small.
Make the gap list
- Names, order, units.
- Types, errors, empty.
- One test per row.
Refuse in your words
- No is an answer.
- Silence is unknown.
- Keep the cause.
Stay thin
- No rules, no retries.
- No foreign types out.
- One adaptee each.
Prove it
- Recording fake.
- Contract suite.
- Nightly, live.