• Home
  • Tutorials
  • Shop
  • AI News
  • Services
  • JaggoRe AITry
Get Started
  • Login
  • Register
Neuraldemy
Cart / 0.00$

No products in the cart.

No Result
View All Result
Get Started
Neuraldemy
Get started
Home Javascript

Strategy Pattern In JavaScript

Neuraldemy by Neuraldemy
March 3, 2026
in Javascript
Reading Time: 2 mins read
A A

A modular checkout processor that delegates payment execution to interchangeable strategy classes (Credit Card, PayPal, Crypto). This allows the application to swap complex payment and verification algorithms dynamically at runtime without altering the core checkout logic.

// Strategy Pattern

interface IPaymentStrategy {
  pay(amount: number): void;
}

class CreditCardPayment implements IPaymentStrategy {
  private cardNumber: string;

  constructor(cardNumber: string) {
    this.cardNumber = cardNumber;
  }

  public pay(amount: number): void {
    console.log(
      `Processing $${amount} via Credit Card ending in ${this.cardNumber.slice(-4)}`,
    );
  }
}

class PayPalPayment implements IPaymentStrategy {
  private emailAddress: string;

  constructor(emailAddress: string) {
    this.emailAddress = emailAddress;
  }

  public pay(amount: number): void {
    console.log(
      `Processing $${amount} via PayPal account: ${this.emailAddress}`,
    );
  }
}

class CryptoPayment implements IPaymentStrategy {
  private walletAddress: string;

  constructor(walletAddress: string) {
    this.walletAddress = walletAddress;
  }

  public pay(amount: number): void {
    console.log(
      `Processing $${amount} via Crypto Wallet: ${this.walletAddress}`,
    );
  }
}

class CheckoutProcessor {
  private paymentStrategy: IPaymentStrategy;

  constructor(strategy: IPaymentStrategy) {
    this.paymentStrategy = strategy;
  }

  public setPaymentStrategy(strategy: IPaymentStrategy): void {
    this.paymentStrategy = strategy;
    console.log("--> Payment strategy updated.");
  }
  public processOrder(amount: number): void {
    console.log("Starting checkout process...");
    this.paymentStrategy.pay(amount);
    console.log("Checkout complete!\n");
  }
}

const creditCardMethod = new CreditCardPayment("4111222233334444");
const checkout = new CheckoutProcessor(creditCardMethod);

checkout.processOrder(150.0);

const paypalMethod = new PayPalPayment("user@exaple.com");
checkout.setPaymentStrategy(paypalMethod);

checkout.processOrder(45.5);Code language: JavaScript (javascript)
Previous Post

Factory Pattern In JavaScript

Neuraldemy

Neuraldemy

This is Neuraldemy support. Subscribe to our YouTube channel for more.

Related Posts

Factory Pattern In JavaScript

Inside OpenAI Military Deal and the Ousting of Anthropic

Nano Banana 2: Google’s New AI Image Generator Just Fixed the Speed vs. Quality Problem

The 5 Best Books for Learning Machine Learning Mathematics

Singleton pattern In JavaScript

Dynamic Array Implementation Modern C++

  • Customer Support
  • Get Started
  • Ask Your ML Queries
  • Contact
  • Privacy Policy
  • Terms Of Use
Neuraldemy

© 2024 - A learning platform by Odist Magazine

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • Tutorials
  • Shop
  • AI News
  • Services
  • JaggoRe AI
  • Login
  • Sign Up
  • Cart
Order Details

© 2024 - A learning platform by Odist Magazine

This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
0