Quick revision
The short answer
I would define ShippingGateway in the order domain's own terms, with quote, createShipment and track methods that use domain types and a small set of domain errors: invalid address, carrier unavailable and rejected. Each carrier gets one adapter that maps requests, responses and error codes and keeps the carrier's SDK types inside it, with timeouts, retries and a circuit breaker at that boundary. The adapter is chosen by configuration or rules, and each adapter is tested with contract tests against recorded or stubbed carrier responses, for example with WireMock.
What to remember
In this answer, we designed adapters for three shipping carriers. ShippingGateway speaks in domain terms with domain types and three normalised errors. One adapter per carrier maps requests, responses, units and error codes and keeps SDK types inside, with timeouts, retries only where safe, and circuit breakers at the boundary. Adapters are selected by configuration or rules rather than conditionals in the domain, and each is tested with contract tests against WireMock stubs plus a few scheduled sandbox tests.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would define ShippingGateway in the order domain's own terms, with quote, createShipment and track methods that use domain types and a small set of domain errors: invalid address, carrier unavailable and rejected. Each carrier gets one adapter that maps requests, responses and error codes and keeps the carrier's SDK types inside it, with timeouts, retries and a circuit breaker at that boundary. The adapter is chosen by configuration or rules, and each adapter is tested with contract tests against recorded or stubbed carrier responses, for example with WireMock.
Background to this question
Integrating external providers is where most production incidents in commerce systems begin: a carrier changes an error code, a timeout blocks checkout, or carrier-specific fields leak into the order code and make every change risky. The adapter pattern gives a clean answer, but the details decide whether it works: what the interface looks like, where failures are translated, and how we test code that talks to systems we do not control. In this answer, we will design the domain interface, write one carrier adapter, normalise errors, add resilience at the boundary, select adapters without conditionals in the domain, and set up contract tests.
1Designing the Interface in Domain Terms
The interface belongs to the order domain, not to any carrier. It speaks in the words the order team uses and returns types the domain owns. This is the most important decision, because it is what keeps carrier details from spreading.
Notice that Money, Address and Parcel are our types. A carrier that wants weights in pounds and another that wants grams both receive our Parcel with a weight in grams, and the adapter converts. The errors are a small, closed set, sealed so that a switch over them is exhaustive, because the domain only needs to decide three things: ask the customer to fix the address, try again or another carrier, or tell the customer this option is not possible.
2Writing One Adapter per Carrier
Each adapter implements ShippingGateway and contains everything specific to its carrier: the SDK or HTTP client, the request and response mapping, unit conversions and error code translation. No carrier type appears in its public methods.
- Order domainuses
ShippingGateway, domain types and three domain errors - ShippingGateway interfacequote,
createShipment, track - Carrier adaptersone per carrier: mapping, units, error codes, resilience
- Carrier SDKs and HTTP APIsvendor types, codes and quirks stay here
4 layers, from the top down. Step through them one at a time.
Carrier types never cross the adapter line upward.
The reason unknown codes map to CarrierUnavailable is safety: a code we have never seen is more likely a carrier problem than the customer's fault, and treating it as transient lets the domain fall back to another carrier. We log the original code so we can add a precise mapping later.
3Resilience at the Adapter Boundary
Remote carriers are slow or down at times, so the adapter is where we apply timeouts, retries and a circuit breaker, a component that stops calling a failing service for a while after repeated failures so that we fail fast instead of waiting. A library such as Resilience4j provides these as decorators. Each carrier gets its own settings, because each has its own latency and limits.
The settings should be explicit and modest. For a quote during checkout, a 2-second timeout and one retry with a short backoff is reasonable, because the customer is waiting. For track, which is idempotent and often runs in the background, three retries are fine. For createShipment, which creates a label and may charge us, we retry only if the carrier supports an idempotency key, a unique id we send so that a repeated request does not create a second shipment. When the circuit is open, the adapter throws CarrierUnavailable immediately, which the domain already knows how to handle.
4Selecting the Adapter Without Conditionals
The domain should never contain if carrier equals FastShip. Instead, all adapters are registered, for example as Spring beans collected into a map, and a small selection component chooses by configuration or rules.
Adding a fourth carrier is then a new adapter class and a configuration change. The domain asks for candidates and, for a quote, can call them in parallel and pick the cheapest that answered within the timeout.
5Testing the Adapters With Contract Tests
Each adapter gets tests that run against stubbed carrier responses rather than the real carrier. WireMock, a library that runs a fake HTTP server, serves responses recorded from the carrier's sandbox or copied from its documentation, including error responses, slow responses and malformed bodies.
On top of that, one shared abstract test class describes the behaviour every ShippingGateway must have, for example that an invalid address always becomes InvalidAddress, and each adapter's test extends it. That is a contract test in the sense that every adapter must pass the same contract. A small number of tests against each carrier's sandbox, run nightly rather than on every build, catch changes on the carrier's side that our recordings do not show yet.
6Follow-up questions
Should the adapter return Optional or a result type instead of throwing?
Either works if it is consistent. A sealed result type, such as QuoteResult with Success and Failure variants, makes failure impossible to ignore and suits flows that compare several carriers. Exceptions are simpler for single calls. What matters is that only domain types and domain errors leave the adapter.
Where do carrier-specific features fit, such as a signature-on-delivery option only one carrier offers?
Model the feature in domain terms, for example a deliveryOptions set on the request, and let each adapter declare which options it supports. The selector then excludes carriers that cannot honour the request, and no carrier name appears in domain logic.
How do you keep recorded responses up to date?
Re-record from the carrier sandbox on a schedule and when the carrier announces API changes, review the diff, and keep the nightly sandbox tests running. A failure there is the signal to refresh the recordings.
7Conclusion
In this answer, we designed adapters for three shipping carriers. ShippingGateway speaks in domain terms with domain types and three normalised errors. One adapter per carrier maps requests, responses, units and error codes and keeps SDK types inside, with timeouts, retries only where safe, and circuit breakers at the boundary. Adapters are selected by configuration or rules rather than conditionals in the domain, and each is tested with contract tests against WireMock stubs plus a few scheduled sandbox tests.
Open with
I'd start from the domain side: ShippingGateway has quote, createShipment and track, takes and returns our own types like Address, Parcel with weight in grams, and Money, and fails with just three domain errors: invalid address, carrier unavailable, and rejected.
Then, if they want more
That's all the order domain needs to decide whether to ask the customer to fix the address, fall back to another carrier, or say the option isn't possible.
Then one adapter per carrier implements that interface and holds everything carrier-specific: the SDK, request and response mapping, unit conversion, and a translation table from carrier error codes to the three domain errors, with unknown codes treated as unavailable and logged. Carrier types never leave the adapter.
The adapter is also where resilience goes: a per-carrier timeout, retries only for idempotent calls or with an idempotency key for shipment creation, and a circuit breaker that throws carrier unavailable fast when the carrier is down.
Adapters are collected into a map by carrier id and chosen by configuration or routing rules, so no carrier names appear in domain code.
For testing, each adapter has WireMock tests with recorded success, error and slow responses, all adapters extend a shared contract test so they behave the same way, and a few nightly tests against each carrier's sandbox catch API drift.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You must integrate three shipping carriers with different APIs, data models and error...": https://scrivio-interview-library.netlify.app/java/structural-patterns/4665ae8e4b/