Skip to main content

Structural and Creational Patterns: Worked Examples

Work through each trace before reading the explanation. Then solve the transfer task without the page open.

Example 1: An adapter converts semantics as well as names​

Provider A quotes integer cents; provider B returns a decimal amount in dollars. Renaming B's method does not make its result interchangeable. A 12.50-dollar quote must become 1250 cents using a declared currency and rounding contract.

The adapter validates currency and range, then converts using decimal-safe arithmetic. It should not silently map provider failure to a free quote. Tests include currency mismatch and malformed amounts as well as normal conversion.

Example 2: Decorator order changes effects​

Let logging and retry both wrap a network call. Logging outside retry records one logical call; logging inside retry records each attempt. Neither arrangement is automatically wrong.

For retries, distinguish logical operation ID from attempt ID and declare which failures are retryable. Retrying a non-idempotent side effect can duplicate it. Structural composition cannot manufacture an idempotency guarantee.

Transfer and self-check​

Where should a total operation deadline sit relative to retries? Check: outside the complete retry loop; individual attempts may also need shorter timeouts.

Evidence to keep​

Save your first attempt, corrected reasoning, and transfer result as checkpoint evidence. A matching final answer without an explanation is insufficient. Continue with the guided lab and use the reading guide to repair a specific gap.