October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Strategy vs Adapter Pattern in JavaScript: When to Use Each

Strategy swaps interchangeable implementations of one behavior; Adapter translates a collaborator's interface into the one your code expects. Here is how to choose between them in JavaScript.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Strategy when your code performs one stable operation and you need to swap which implementation carries it out. Use Adapter when a collaborator exists but its interface differs from the one your code expects. In short, a Strategy selects or encapsulates behavior, and an Adapter translates a contract. If a small function or a direct call already makes the design obvious, you do not need either pattern.

Both names come from Design Patterns: Elements of Reusable Object-Oriented Software (1995) by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, which catalogs 23 core object-oriented patterns. That catalog is a historical reference. It does not show that either pattern is more common or more effective than a plain function.

As an Amazon Associate I earn from qualifying purchases.

Strategy: varying behavior behind a stable call

The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. That is the wording of the glossary definition. The Project Management Institute’s Disciplined Agile strategy pattern reference, as excerpted in search results, describes the same motivation in terms of consumers: a single behavior has varying implementations, and the consumers should be decoupled from any particular one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A function-based shape

In JavaScript, the simplest Strategy is a set of functions that share a signature. The caller passes one in and never needs to know how it works. The following example is an illustrative sketch written for this article, not code taken from a source:

const pricingStrategies = {
  standard: (subtotal) => subtotal,
  member: (subtotal) => subtotal * 0.9,
  seasonal: (subtotal) => subtotal * 0.8,
};

function totalFor(subtotal, strategy) {
  return strategy(subtotal);
}

const total = totalFor(100, pricingStrategies.member); // 90

The operation, totalFor, stays fixed. Only the rule changes. Adding a new pricing rule means adding one entry to the object, with no edits to totalFor or its callers.

Use an object with methods instead of a bare function when a strategy needs several related operations or keeps its own state, such as a cache, a configuration, or a counter.

When a plain conditional is enough

Not every branch deserves a strategy. The PMI reference names simple branching as the procedural equivalent of this pattern, which means the branching is the same idea written without a boundary. A conditional is usually the better choice when:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • The set of options is fixed and small.
  • The decision is made in one place and nowhere else.
  • New options are unlikely to appear.

Introduce strategy functions or objects when the variation has to be hidden from the caller, chosen independently of the code that uses it, or extended by people who never edit the core function. If none of those apply, an if or switch is clearer and adds no indirection.

Adapter: translating a collaborator’s interface

The glossary defines Adapter as converting the interface of a class into another interface that clients expect. The JavaScript tutorial on this pattern frames it as a wrapper. Its payment examples present one common payment interface and delegate to a legacy system’s makePayment method or a third-party service’s chargeCard method. The adapter can also normalize the fields a result returns, so the application reads one shape.

A wrapper around a legacy method

The sketch below shows the idea. The legacy method’s name, arguments, and result fields differ from what the application wants, so the adapter maps between them. As with the Strategy example, this is an illustrative sketch, not production code:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
function makeLegacyPaymentAdapter(legacy) {
  return {
    processPayment({ amount, currency = "USD" }) {
      const result = legacy.makePayment(amount);
      return {
        status: result.success ? "completed" : "failed",
        transactionId: result.transactionId,
        amount,
        currency,
      };
    },
  };
}

Application code calls processPayment and never sees makePayment. Swapping the legacy service for a new one means writing a new adapter, not changing every caller.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where adapters belong, and what they cannot fix

Place adapters at the edges of your system, where code meets a vendor or an old module. Keep vendor-specific names, units, and error shapes inside the adapter so the rest of the application speaks one contract. Write down the decisions the adapter makes, including default values such as the currency above, unit conversions, and how errors are reported.

An adapter changes names and shapes. It does not change meaning. If two services handle failures differently, or one charges a card before confirming an order while the other does not, wrapping them behind one method hides that gap. Resolving it takes an explicit policy, such as timeouts, retries, or idempotency rules, and the adapter is only the place where that policy is applied. The tutorial’s example illustrates interface translation. It does not document how any real payment system behaves in production.

Adapter versus Facade

Both patterns can wrap complexity, which makes them easy to confuse. The glossary separates them by intent. An Adapter converts an interface that clients already expect to be a different one. A Facade provides a unified, higher-level interface to a subsystem. An adapter usually wraps one collaborator to match a contract; a facade usually wraps several parts to offer a simpler entry point.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Side-by-side comparison

Axis Strategy Adapter
Main intent Make implementations of a behavior interchangeable. Make incompatible interfaces work together.
What changes Which rule or algorithm the client uses. How a collaborator is called and how its values are translated.
Client contract A stable operation with selectable implementations. A stable expected interface presented over a different interface.
Typical trigger A behavior such as pricing, sorting, or validation varies by context. These are illustrative examples, not a complete list from the sources. A legacy or third-party API, or a data shape, does not match the application’s contract. The cited tutorial uses payment interfaces.
Common mistake Creating strategy types for fixed branching that never changes, which adds indirection with no real variation. Letting vendor-specific details leak into callers, or renaming methods while ignoring differences in meaning.

Deciding which pattern fits

  • Does the caller already have the operation it needs, with several interchangeable ways to do it? That points to Strategy.
  • Does a collaborator exist whose methods, arguments, or result shape differ from what your code expects? That points to Adapter.
  • Is the choice fixed, local, and unlikely to grow? A conditional is enough.
  • Are you only renaming a single function call with no change in meaning? A direct wrapper function may be clearer than a named pattern.

Strategy answers the question of which behavior to run. Adapter answers the question of how to talk to something that does not speak your language. Keeping those questions separate is what makes the choice straightforward.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.