7
← Back to Pattern Arsenal
creational

Factory Method

must know

Stage 1: Problem

The Problem

You are building a payment service for an e-commerce platform. Right now you only support Stripe. The code is simple:

typescript
1class PaymentService {
2 async processPayment(amount: number, currency: string, token: string) {
3 const stripe = new Stripe(process.env.STRIPE_KEY!);
4 const charge = await stripe.charges.create({ amount, currency, source: token });
5 return { id: charge.id, status: charge.status };
6 }
7}

Then requirements grow:

  1. India launch — you need Razorpay for UPI and domestic card payments.
  2. Europe launch — you need PayPal support.
  3. Merchant choice — each merchant configures their preferred gateway in the admin panel.
  4. New gateways keep coming — the business team wants to add Adyen, Square, and local processors. Each has a different SDK, different auth flow, different response shape.

You need a system where adding a new payment gateway does not require changing the core payment processing logic.

How would you solve this? Think for a moment before scrolling. Write your approach below.


1 / 10 — Problem