ProgramGuru

Adapter Pattern

Adapter: start from the problem

See the responsibilities

ApplicationRequests priceCents
AdapterTranslates the interface and units
Legacy catalogReturns dollars
Application resultReceives cents under its contract
Application: Requests priceCents → Adapter: Translates the interface and units → Legacy catalog: Returns dollars → Application result: Receives cents under its contract

The application expects prices in cents, but an older catalog API returns dollars. Rewriting every caller around the older interface spreads the mismatch through the codebase.

This is a structural pattern lesson. Follow the responsibilities, inspect the small JavaScript example and compare the design choices. The example is local teaching code; its boundary is explained before you apply the idea to a real service.

What you’ll learn

  • Explain the problem Adapter addresses and the roles involved.
  • Follow the example and distinguish this pattern from nearby alternatives.
  • Choose when the benefit justifies the extra responsibilities and testing.

How the responsibilities work

Adapter presents the interface a client expects while translating to an existing component. The adapted component can keep its original interface. The adapter owns the conversion and must preserve meaning, including units and error handling.

Our application calls priceCents. The adapter calls the older priceDollars method and converts the chosen simple value. A real financial conversion needs a deliberate decimal and rounding contract.

Follow a small example

Read the JavaScript from top to bottom and compare the expected output. The values are chosen to make this pattern’s responsibility visible. This example runs in one local execution context; it does not create remote services, durable storage or production access controls.

JavaScript · local teaching example

const legacyCatalog = {priceDollars: () => 12.5};
const catalogAdapter = {
  priceCents() { return Math.round(legacyCatalog.priceDollars() * 100); }
};
console.log(catalogAdapter.priceCents());

Expected output

1250

Choose a design and inspect the consequence

Compare the design choices

The old API reports a different currency.
The legacy component has many callers.

Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.

This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.

Use the consequences to explain your decision, including the simpler alternative.

Apply the pattern to two situations. Select one option in each situation to build a decision record. Every option and its consequence stays visible, and you can change your choices.

When it helps—and what it costs

Use it to integrate a useful existing component whose interface differs from the client’s expectation. Keep conversions together and test representative error and boundary values.

An adapter can hide important semantic differences, such as currency, ordering or side effects. An interface with the same method names is not enough if the promises differ. Avoid a growing adapter that quietly owns every business rule.

Distinguish the neighbouring patterns

Bridge is usually designed to let two dimensions evolve independently. Adapter reconciles an existing mismatch. An Anti-Corruption Layer protects a wider business model across a system boundary.

Follow Bridge’s independent variation, translation at a business boundary, gradual legacy replacement to compare the specific responsibility before combining patterns.

Check your pattern reasoning

What must the price adapter preserve?
Choose one answer. You can try again.

Answer and explanation

The application’s price meaning, including units.

Method translation must also preserve semantics.

Check your pattern reasoning

When is Adapter especially useful?
Choose one answer. You can try again.

Answer and explanation

An existing component has a useful but incompatible interface.

The wrapper reconciles the mismatch for clients.

Name the responsibility before choosing the pattern

For Adapter, describe the problem, the participating roles and the observable contract. Use the example as a starting point and test the boundaries identified in the trade-offs. A pattern name alone does not establish correctness or performance.

Return to the design-pattern guide to compare families, or apply the idea alongside gradual legacy replacement.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Bridge Pattern →