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.
Appearance
ADAPTER / THE IDEA IN A MINUTE
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.
THE STRUCTURE / FOUR ROLES
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
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
read(): WeatherReadingA reading contains celsius and condition: sunny or rainy. This is the interface the adapter must satisfy.
03 / ADAPTERTranslation lives here
feed: LegacyForecastFeedImplements 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
getForecast(): LegacyForecastReturns 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.
ONE REQUEST ACROSS THE BOUNDARY
Change the sample to inspect the translation. The four roles stay the same.
Setup has supplied a FahrenheitAdapter as its WeatherSource.
It asks its existing feed for the vendor’s response.
temp_f: 64.4, sky_code: 1It 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
These local sample services describe the same weather in different ways. Request a reading and follow the data returning to the app.
THE PROVIDER
getForecast(){
"temp_f": 64.4,
"sky_code": 1
}Sample response · vendor format
THE ADAPTER
getForecast() → read()
(°F − 32) × 5 / 9 → °C
sky_code → condition
Wrap the existing feed. Translate at the boundary.
THE APP’S DISPLAY
source.read()—
Waiting for a reading
Expects Celsius + sunny / rainy.
Display rounds to one decimal.
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.
Connect an existing library, vendor service or legacy system without teaching every client its conventions.
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.
Start with a compatible service, feel the cost of a new format, then move translation into an adapter.
Live weather conversion · optional practiceBACK FOR A REFRESHERReview the roles, actual conversion code, contract guarantees and simpler alternatives.
TypeScript + Kotlin · bookmarkable