← Back to PatternPip

ADAPTER / THE IDEA IN A MINUTE

Different languages.
One conversation.

Adapter lets an existing object work with a client that expects a different interface.

Wrap the object with a small translator. The client uses its familiar contract. The adapter translates calls for the existing service and responses for the client.

What does not fit?
An existing service has a different interface or data format.
What stays stable?
The interface the client already expects.
Where does translation go?
In an adapter that wraps the incompatible service.

THE STRUCTURE / FOUR ROLES

The app keeps its interface. The adapter speaks both.

The weather panel expects WeatherSource. The existing forecast feed has a different method and data format. FahrenheitAdapter implements the app’s interface while holding a reference to that feed.

01 / CLIENTUnchanged

Weather panel

readPanel(source: WeatherSource)

Calls source.read(). It expects Celsius and a named condition, without knowing the vendor’s method or codes.

02 / TARGET INTERFACEApp’s contract

WeatherSource

read(): WeatherReading

A reading contains celsius and condition: sunny or rainy. This is the interface the adapter must satisfy.

03 / ADAPTERTranslation lives here

FahrenheitAdapter

feed: LegacyForecastFeed

Implements WeatherSource and holds the existing feed. Its read() calls feed.getForecast(), then translates the response.

The panel sees a WeatherSource. The feed receives its familiar getForecast() call.

04 / EXISTING SERVICEUnchanged · adaptee

LegacyForecastFeed

getForecast(): LegacyForecast

Returns temp_f in Fahrenheit and a numeric sky_code. It does not implement WeatherSource.

The adapter uses this object as it is; it does not rewrite the vendor service.

Read the arrows as relationships: the panel uses WeatherSource; FahrenheitAdapter implements it and holds the existing feed. Only the adapter knows both interfaces.

ONE REQUEST ACROSS THE BOUNDARY

Different response. Same contract.

Change the sample to inspect the translation. The four roles stay the same.

  1. 1. The panel calls read()

    Setup has supplied a FahrenheitAdapter as its WeatherSource.

  2. 2. The adapter calls getForecast()

    It asks its existing feed for the vendor’s response.

    temp_f: 64.4, sky_code: 1
  3. 3. The adapter returns a WeatherReading

    It converts Fahrenheit to Celsius and maps the numeric code to a condition.

    celsius: 18, condition: "sunny"

64.4 °F → 18 °C using (°F − 32) × 5 / 9. Code 1 → sunny. The panel receives the format it already understands.

Try the demo below to compare the compatible station with the adapted legacy feed.

FOLLOW THE RESPONSE

Different format. Same display.

Try it yourself

These local sample services describe the same weather in different ways. Request a reading and follow the data returning to the app.

THE PROVIDER

Legacy Forecast

getForecast()
{
  "temp_f": 64.4,
  "sky_code": 1
}

Sample response · vendor format

THE ADAPTER

FahrenheitAdapter

getForecast() → read()

(°F − 32) × 5 / 9 → °C

sky_code → condition

Wrap the existing feed. Translate at the boundary.

THE APP’S DISPLAY

Weather panel

source.read()

—

Waiting for a reading

Expects Celsius + sunny / rainy.
Display rounds to one decimal.

What if the provider sends a code we don’t understand?

Request again. An adapter cannot safely guess whether an unknown code means sun or rain.

Request weather to follow the response from provider to display.

Try this: read the Fahrenheit feed, then switch to Celsius Station. Compare the raw response with the display’s result.

Reduced motion is on; results appear immediately. The caller still chooses the provider. The adapter owns the translation.

Useful when interfaces do not fit.

Connect an existing library, vendor service or legacy system without teaching every client its conventions.

Start with the smallest useful boundary.

A mapping function may be enough for one conversion. An adapter adds a wrapper to maintain, and cannot make missing data or incompatible behavior disappear.

Choose your next step.