Skip to content
Strategy Pattern: Turn if Branches into Classes

Strategy Pattern: Turn if Branches into Classes

Learn how to split a payment function full of if branches into replaceable parts with the Strategy pattern, step by step in TypeScript.

When it hurts

Every new payment method…

Let's take the payment code of an online shop as an example. Every time a payment method is added, the if statement gets another branch. Even adding a single method means rewriting a function that already works. As a result, every change forces you to retest all of the payment methods.

  • One more if branch per payment method
  • Each addition edits a working function
  • Every change means retesting everything

Payment code written with branches

Here is the code written with branches. The pay function holds the logic for every payment method. Even the list of payment methods is managed as a string type.

before.ts

 1type Method = "card" | "bank" | "points";
 2
 3function pay(method: Method, amount: number): string {
 4  if (method === "card") {
 5    return `Card: $${amount} (fee $${(amount * 0.03).toFixed(2)})`;
 6  } else if (method === "bank") {
 7    return `Bank transfer: $${amount + 2}`;
 8  } else {
 9    return `Points: ${amount} pt`;
10  }
11}
12
13console.log(pay("card", 100));
14console.log(pay("bank", 100));
15console.log(pay("points", 100));

The idea behind Strategy

Swap behavior as a part

With the Strategy pattern, the behavior you want to switch hides behind a shared interface. The code that runs the payment only knows the interface. Each payment method becomes a part that implements the same interface.

Swap behavior as a part

Implementing it in TypeScript

A shared interface and its implementations

First, define a shared interface for payments. It is a simple interface with a single pay method. Each payment method becomes its own class that implements this interface.

after.ts

 1interface PaymentStrategy {
 2  pay(amount: number): string;
 3}
 4
 5class CardPayment implements PaymentStrategy {
 6  pay(amount: number): string {
 7    return `Card: $${amount} (fee $${(amount * 0.03).toFixed(2)})`;
 8  }
 9}
10
11class BankPayment implements PaymentStrategy {
12  pay(amount: number): string {
13    return `Bank transfer: $${amount + 2}`;
14  }
15}
16
17class PointPayment implements PaymentStrategy {
18  pay(amount: number): string {
19    return `Points: ${amount} pt`;
20  }
21}

The caller just receives a part

The Checkout class simply hands the work to the strategy it receives. With setStrategy, you can even swap the part while the program is running. The caller switches payment methods just by passing a different part.

after.ts

 1class Checkout {
 2  private strategy: PaymentStrategy;
 3
 4  constructor(strategy: PaymentStrategy) {
 5    this.strategy = strategy;
 6  }
 7
 8  setStrategy(strategy: PaymentStrategy): void {
 9    this.strategy = strategy;
10  }
11
12  run(amount: number): string {
13    return this.strategy.pay(amount);
14  }
15}
16
17const checkout = new Checkout(new CardPayment());
18console.log(checkout.run(100));
19checkout.setStrategy(new BankPayment());
20console.log(checkout.run(100));
21checkout.setStrategy(new PointPayment());
22console.log(checkout.run(100));

Output

When you run it, you get the same result as the version with branches. We changed the structure without changing the behavior. You can run this sample code yourself in the Ginupy Code app.

1$ node after.ts
2Card: $100 (fee $3.00)
3Bank transfer: $102
4Points: 100 pt

Adding a new payment method

Let's compare what it takes to add a new payment method. With branches, you have to modify the existing pay function. With the Strategy pattern, you only add one new class.

Before: branches

1function pay(method: Method, amount: number): string {
2  if (method === "card") {
3    return `Card: $${amount} (fee $${(amount * 0.03).toFixed(2)})`;
4  } else if (method === "bank") {
5    return `Bank transfer: $${amount + 2}`;
6  } else {
7    return `Points: ${amount} pt`;
8  }
9}

After: Strategy

1class PointPayment implements PaymentStrategy {
2  pay(amount: number): string {
3    return `Points: ${amount} pt`;
4  }
5}

Summary

Real-world examples

The Strategy pattern shows up in libraries you probably already use. Passport.js, an authentication library for Node.js, plugs in a separate "strategy" for each login method. Workbox, which handles caching in web apps, lets you pick a caching strategy as a part. Passing a compare function to an array's sort follows the same idea: the ordering comes from outside.

  • Passport.js: a Strategy for each login method
  • Workbox: caching strategies such as CacheFirst
  • Array.prototype.sort: pass the ordering as a function

When to use it, and when not to

When you have several ways to do the same job, and more are likely to come, the Strategy pattern pays off. It also fits when you want to switch behavior while the program is running. On the other hand, if there are only two branches and no plans to add more, a plain if statement is often easier to read.

  • ✅ Several ways to do the same job, and more to come
  • ✅ You want to switch behavior at runtime
  • ⚠️ Two fixed branches? A plain if will do

The sample code on this page runs as is with node after.ts on Node.js 24 or later.

Narration in this video uses a synthetic (AI-generated) voice.