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
Deep dives
Compare the options before you open them, then say which you would pick.
Dig
Where strong interviewers dig
| They ask | The short answer | Page |
|---|---|---|
| Timeout: what now? | Unknown is a third answer | Meanings |
| No idempotency at the vendor? | Search first, one worker per key | Meanings |
| Why not inherit? | Only to override an inner step | Forms |
| Removing a wrapped listener? | Remember the adapter in a weak map | Forms |
| Replace the vendor? | Switch by hash, pin each key | Replacing |
| Where do retries live? | In a wrapper around it | Failure |
| How do you trust your fake? | It passes the contract suite | Contract tests |
One vendor can refund partly, the other only in full. What goes into the target?
Breaksrefund(receipt, amount) and an exception at the weak vendor
- How
- Callers learn it in production.
- Limit
- The interface promises what one class can not do. Liskov is broken.
- Choose when
- Never silently.
WorksOnly the common part: full refund
- How
- The lowest common part.
- Limit
- The strong vendor's ability is lost, people refund by hand.
- Choose when
- If partial refunds are rare.
StrongA separate capability
- How
PartialRefundsas a second interface, orcapabilities()that the screen asks before it shows the button.- Limit
- Callers have two paths, openly.
- Choose when
- When the difference matters to people.