Adapter
Translate an existing API into the contract your client already understands. An adapter preserves the client’s vocabulary while isolating vendor-specific units and return shapes.
What you’ll understand
- Translate an existing API into the contract your client already understands. An adapter preserves the client’s vocabulary while isolating vendor-specific units and return shapes.
- ShippingAdapter wraps LegacyShipping, converts kilograms to grams, and rounds hours up to whole delivery days.
- Evaluate the tradeoff: Translation adds a boundary to maintain. Keep business pricing policy out of the adapter and document rounding rules. A facade simplifies a subsystem; an adapter changes its contract.
Useful first: Classes and Objects
The idea and when to recognize it
Translate an existing API into the contract your client already understands. An adapter preserves the client’s vocabulary while isolating vendor-specific units and return shapes.
- Requirement: A checkout asks for a shipping quote using kilograms and expects integer cents plus delivery days. The vendor accepts grams and reports transit hours.
- Change to anticipate: Integrate the legacy SDK without modifying ShippingClient, including fractional kilogram weights and transit times that are not whole days.
- Design goal: ShippingAdapter wraps LegacyShipping, converts kilograms to grams, and rounds hours up to whole delivery days.
Start with the concrete problem
A checkout asks for a shipping quote using kilograms and expects integer cents plus delivery days. The vendor accepts grams and reports transit hours.
Integrate the legacy SDK without modifying ShippingClient, including fractional kilogram weights and transit times that are not whole days.
| Direct implementation | Revised design |
|---|---|
| Relabeling fields without converting units silently changes the shipping price or delivery promise. | ShippingAdapter wraps LegacyShipping, converts kilograms to grams, and rounds hours up to whole delivery days. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | Translation adds a boundary to maintain. Keep business pricing policy out of the adapter and document rounding rules. A facade simplifies a subsystem; an adapter changes its contract. |
Responsibilities and contracts
ShippingAdapter wraps LegacyShipping, converts kilograms to grams, and rounds hours up to whole delivery days.
- ShippingClient: Depends only on the normalized quote contract. Public operations: quote(order)
- ShippingAdapter: Translates the shipping contract. Public operations: quote(order)
- LegacyShipping: Owns the vendor API. Public operations: estimate(grams, hours)
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A sensor adapter can convert a device’s millivolt readings into the temperature contract expected by a monitoring dashboard.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
Translation adds a boundary to maintain. Keep business pricing policy out of the adapter and document rounding rules. A facade simplifies a subsystem; an adapter changes its contract.
Common mistake: Relabeling fields without converting units silently changes the shipping price or delivery promise.
- Explain the choice: ShippingAdapter wraps LegacyShipping, converts kilograms to grams, and rounds hours up to whole delivery days.
- Challenge your choice: Translation adds a boundary to maintain. Keep business pricing policy out of the adapter and document rounding rules. A facade simplifies a subsystem; an adapter changes its contract.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…