Adapter pattern
Адаптер
A design pattern that lets code use an existing component through the interface it already expects. The adapter translates calls and data so the two can work together without changing the existing component.
What problem does an adapter solve?
Suppose your application calls notify(recipient, message), but a notification SDK only accepts send(payload). The SDK can do the job; it just doesn't accept calls in the form your application uses. An adapter provides notify, builds the SDK's payload, and calls send. The application keeps its interface, and the SDK doesn't have to change. Adapter is a structural design pattern because it describes how pieces of code fit together.
How it works
Three roles matter: the client makes the call, the target interface defines what the client expects, and the adaptee is the existing component. The adapter implements the target interface and delegates the underlying work to the adaptee. It can translate argument names, data formats, return values, and errors. The interface can be a language construct, such as a Java interface, or simply a contract the client relies on.
An object adapter holds a reference to the adaptee. That's composition, and it lets you pass in a different instance without changing the adapter. A class adapter uses inheritance to reuse the adaptee's implementation while exposing the target interface. Whether that approach is practical depends on the language and on whether the adaptee can be subclassed.
Example: adapting a temperature sensor
A dashboard expects read_celsius(), but an older sensor exposes only read_fahrenheit(). The adapter is the one place that knows about the sensor's units:
class CelsiusSensorAdapter:
def __init__(self, sensor):
self.sensor = sensor
def read_celsius(self) -> float:
fahrenheit = self.sensor.read_fahrenheit()
return (fahrenheit - 32) * 5 / 9
Pass CelsiusSensorAdapter(old_sensor) to the dashboard. It can use that object or a newer sensor that already supports read_celsius(). An adapter test can supply a fake sensor that returns 32 and check that the result is 0.
Where it helps and where it falls short
Adapters are useful when introducing a vendor SDK, switching libraries, or migrating legacy code in stages. The translation lives at the integration boundary rather than in every caller. But matching the method signature isn't enough: if the target promises a fresh reading and the sensor can only return a cached one, a unit conversion doesn't fulfill the contract. Avoid quietly hiding such differences, for example by turning a sensor failure into a valid-looking reading of zero.
In an interview
Identify the two interfaces, show where the translation happens, and explain what stays unchanged. If asked to compare wrappers, focus on intent: an adapter makes an incompatible interface usable, a proxy typically keeps the interface and controls access, and a decorator keeps the interface while adding behavior. A facade offers a simpler entry point to a subsystem rather than matching a specific interface a client already expects.