AlgoViz

Adapter pattern

easy 50 min

The 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 askThe short answerPage
Timeout: what now?Unknown is a third answerMeanings
No idempotency at the vendor?Search first, one worker per keyMeanings
Why not inherit?Only to override an inner stepForms
Removing a wrapped listener?Remember the adapter in a weak mapForms
Replace the vendor?Switch by hash, pin each keyReplacing
Where do retries live?In a wrapper around itFailure
How do you trust your fake?It passes the contract suiteContract 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
PartialRefunds as a second interface, or capabilities() that the screen asks before it shows the button.
Limit
Callers have two paths, openly.
Choose when
When the difference matters to people.