Adapter pattern 00:00:00 One client, two languages By the end of this lesson, you should be able to draw an adapter from a caller's request, put each translation in one place, and explain what to do when the external system cannot give you a definite answer. You tap resume at ninety seconds. The button sends the number ninety straight to a decoder that counts milliseconds. Before we change a line of code, where in the movie will it actually start? Ninety milliseconds is nine hundredths of a second. It looks as if the film started over. Nothing crashed. Both sides accepted a number; they just meant different things by it. Let's give that mismatch a place to live. 00:00:42 Try the coupling at the village well Picture a village pump with a screw thread, beside a hose whose end has two claws. Hold the claws against the thread. They turn and slip, so water leaks. The hose is fine; the pump is fine. What shape would let these two exact pieces work together? Put a coupling between them. Its hose side has slots for the claws. Its pump side screws onto the thread. Follow the water back through the two joins. The client recognizes its usual fitting, and the coupling alone knows both shapes. In software, the interface the client expects is called the target; the existing component behind the coupling is the adaptee. Suppose the engine asks for ten litres and the pump delivers two litres per stroke. The coupling's operator turns a quantity into five strokes. But if the well is dry, no translation can supply water. That limitation matters when a payment provider remains silent, too. 00:01:39 Follow one real playback call Take the same request back to our player. The resume button asks the adapter to play the file at ninety seconds. The adapter multiplies once and calls the engine with ninety thousand milliseconds. Add a second button, replay from bookmark, and it can use the same operation without copying that multiplication. A different decoder wants three calls: load, seek and run. It still has yesterday's concert loaded. You ask for a new film and load returns unsupported format. If the adapter calls run anyway, what might the viewer see? Yesterday's concert. The adapter must stop the sequence on the load failure and report an unsupported file to the client. Test with a recording decoder: when load fails, neither seek nor run should appear in its call log. That proves you translated the protocol as well as the units. 00:02:36 Try a payment where silence matters Here's a second example. Checkout uses integer minor units. One bank instead requests a decimal amount as text. Twelve hundred fifty US cents becomes twelve dollars and fifty cents. The same integer for Japanese yen stays twelve hundred fifty yen. Store the currency with the amount; a universal divide by a hundred gives a wrong charge. Now the bank connection drops before the receipt comes back. A common answer is to return declined and try again. But a bank could have moved the money. The adapter should report an unknown outcome, preserve the caller's request identifier, and allow the payment workflow to reconcile the charge. If both pieces belong to your team, use identical units, and only a method name differs, a direct rename may be cheaper. Introduce an adapter when an actual boundary or recurring translation justifies it. You should be able to name the mismatch before you name the pattern. 00:03:35 Draw it under interview pressure An interviewer hands you a temperature display that expects Celsius and a sensor that reports Fahrenheit or disconnected. Sketch the caller, the contract and the sensor. Which component converts sixty-eight degrees Fahrenheit, and what should the display receive if the wire comes loose? Put the arithmetic in the sensor adapter. Sixty-eight degrees Fahrenheit becomes twenty degrees Celsius. Disconnected becomes an explicit unavailable reading. Zero would be a valid temperature, so returning zero hides the difference. Test both inputs against the same display contract and explain why the display never needs a vendor-specific error code. Start with what the caller must be able to ask. Write down what the provider actually offers. Trace one ordinary input, then one failure the two systems express differently. If the caller can keep its vocabulary and the new provider keeps its semantics, your adapter has done its job.