Skip to main content

Adapters preserve meaning, not just shape

What this concept is​

An Adapter translates a source contract into a target contract. Fields, units, errors, identity and ownership matter as much as method names. A Facade instead offers a simpler entry point to a subsystem; one wrapper can serve both intentions.

Why it matters here​

A plausible-looking converted value can be wrong by a unit scale or a rounding rule. The lab makes conversion an independently checked boundary.

Concrete example​

The source record is exactly amount and currency. For USD amount "12.50", the target is integer 1250 cents. "12.5000" is also exact cents; "12.501" is rejected rather than rounded. The teaching grammar accepts 1–12 ASCII whole digits and optionally 1–4 fractional digits, with no sign, whitespace or exponent. Other currencies and extra fields are rejected.

Common confusion / misconception​

Converting a binary float to Decimal does not recover an intended decimal string. Decimal arithmetic also has a context precision; merely choosing Decimal does not guarantee every later operation is exact. A schema change cannot be silently treated as a harmless refactor.

How to use it​

Validate shape, currency and syntax first. Parse the decimal string exactly. Use an exact integer ratio and check whether multiplying the numerator by 100 is divisible by the denominator; a remainder means fractional cents. The input-length bound prevents absurd exponents or unbounded digit input. Do not mutate the source record. This deliberately narrow contract teaches boundary design, not general monetary processing.

Check yourself​

Why does "0.0100" pass but "0.0101" fail? Why should neither be retried after conversion failure?

Mini drill or application​

Write success and rejection examples before implementation. Change the target to thousandths and identify the grammar, conversion, tests and documentation that must change. Do not claim compatibility merely because the return type remains integer.

Read this only if stuck​

Use the existing reference map and composition source guide. The GoF book index supplies vocabulary; the lab’s contracts define the concrete behavior.