Adapter pattern
easy 50 minThe Adapter pattern makes an existing class fit the interface that its client expects, without changing either of them.
Watch lesson4:39
Replacing a vendor
Run two adapters side by side, compare, and switch without a big day.
The point: the adapter is the seam at which you can replace a vendor while people are paying. Plan the seam, the comparison and the way back.
Border
Keep the foreign model out
When the other side has its own idea of a customer or a price, its words leak through names, enums and nulls. A whole layer of adapters and translators at that border is called an anti-corruption layer. Eric Evans described it in Domain-Driven Design. The [Microsoft architecture guide](https://learn.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer) describes it as a facade or adapter layer between systems that do not share the same meaning.
The seam
1/3Before
The checkout knows the interface. Behind it was one adapter.
usesimplementshas (lives on)
PaymentGateway. Those two arrows are left out to keep the picture readable.Code
A switch that remembers
import hashlibclass SwitchingGateway: """Sends a share of new keys to the new vendor. A key never changes its vendor.""" def __init__(self, old, new, share, home=None): self._old, self._new, self._share = old, new, share self._home = {} if home is None else home # a table in real life def charge(self, money, key): name = self._home.setdefault(key, self._choose(key)) gateway = self._new if name == "new" else self._old return gateway.charge(money, key) def _choose(self, key): number = int(hashlib.sha256(key.encode()).hexdigest(), 16) % 100 return "new" if number < self._share else "old"class Recorder: def __init__(self): self.keys = [] def charge(self, money, key): self.keys.append(key) return keyold, new, table = Recorder(), Recorder(), {}first = SwitchingGateway(old, new, share=10, home=table)for n in range(1000): first.charge(1, f"k{n}")print("new vendor got", len(new.keys), "of 1000")assert 60 < len(new.keys) < 140before = list(new.keys)second = SwitchingGateway(old, new, share=50, home=table) # raise the sharesecond.charge(1, before[0])assert new.keys[-1] == before[0]on_old = old.keys[0]second.charge(1, on_old)assert old.keys[-1] == on_old # a retry stays where it beganhome table is the point. If a retry of a half-finished payment went to the other vendor, the customer could pay twice.The way over
- 1
Contract green
The new adapter passes the same contract test as the old one, against the vendor's test system.
- 2
Compare reads
Send questions without effect (list, find, price) to both, answer from the old one, log differences. Never send a charge to both.
- 3
One percent
By hash of the key, not by chance, so that a retry goes the same way.
- 4
Watch four numbers
Accepted share, unknown share, p99 time, differences in the nightly comparison with the vendor's own list.
- 5
Raise, with a way back
5, 25, 50, 100. Lowering the share affects only new keys.
- 6
Keep the old one for reading
Refunds of old payments go to the vendor that took the money, for as long as refunds are possible.
Is the new vendor ready for more?
What people forget
| Thing | Why it bites |
|---|---|
| Refunds and disputes | They need the vendor of the original payment, months later |
| Reports | Finance reads vendor references. Store vendor name and reference together |
| Webhooks | The other direction needs an adapter too: their event becomes our event |
| Test cards and sandboxes | Each vendor has its own, the contract test must get them as data |
| Rate limits | Two vendors, two limits. The adapter should slow down, not the checkout |