Simple FactoryVSFactory Method
The Problem
Scenario A — Simple Factory territory:
You need a utility that takes a string like "stripe" or "razorpay" and returns the correct PaymentGateway instance. There is no need for subclassing — one function or static method with a switch does the job.
Scenario B — Factory Method territory:
You are building a plugin system for payment processing. Each payment module (StripeModule, RazorpayModule) is a subclass that overrides a createGateway() method. The base class defines the workflow; the subclass only decides which gateway to create.
Why They Look Alike
Both produce objects based on some condition. Both avoid new ConcreteClass() in the client code. People call both of them "the factory pattern" casually, which causes all the confusion.
The Fundamental Difference
Simple Factory is a function/method that picks a class. Factory Method is a design pattern where subclasses override a creation method.
Simple Factory: one function with a switch. No inheritance.
Factory Method: inheritance is the mechanism — the base class calls this.createX() and the subclass supplies the implementation.
Decision Rule
| Clue | Pattern |
|---|---|
| "One function that returns different objects based on a param" | Simple Factory |
| "Base class defines a workflow, subclass decides what to create" | Factory Method |
| "No inheritance involved in the creation" | Simple Factory |
| "Adding a new type means adding a new subclass" | Factory Method |
| "A static method or standalone function" | Simple Factory |
| "The creator has other behaviour beyond just creating" | Factory Method |
Side-by-Side Code
Simple Factory — a function with a switch
| 1 | interface PaymentGateway { |
| 2 | charge(amount: number): string; |
| 3 | } |
| 4 | |
| 5 | class StripeGateway implements PaymentGateway { |
| 6 | charge(amount: number) { return `Stripe: $${amount}`; } |
| 7 | } |
| 8 | |
| 9 | class RazorpayGateway implements PaymentGateway { |
| 10 | charge(amount: number) { return `Razorpay: ₹${amount}`; } |
| 11 | } |
| 12 | |
| 13 | // Simple Factory: just a function |
| 14 | function createGateway(type: string): PaymentGateway { |
| 15 | switch (type) { |
| 16 | case "stripe": return new StripeGateway(); |
| 17 | case "razorpay": return new RazorpayGateway(); |
| 18 | default: throw new Error(`Unknown gateway: ${type}`); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Usage |
| 23 | const gw = createGateway("stripe"); |
| 24 | gw.charge(100); |
Factory Method — subclass overrides creation
| 1 | interface PaymentGateway { |
| 2 | charge(amount: number): string; |
| 3 | } |
| 4 | |
| 5 | // Base class with a workflow that calls the factory method |
| 6 | abstract class PaymentModule { |
| 7 | abstract createGateway(): PaymentGateway; |
| 8 | |
| 9 | processPayment(amount: number): string { |
| 10 | const gateway = this.createGateway(); // factory method call |
| 11 | const result = gateway.charge(amount); |
| 12 | console.log(`Processed: ${result}`); |
| 13 | return result; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | class StripeModule extends PaymentModule { |
| 18 | createGateway() { return new StripeGateway(); } |
| 19 | } |
| 20 | |
| 21 | class RazorpayModule extends PaymentModule { |
| 22 | createGateway() { return new RazorpayGateway(); } |
| 23 | } |
| 24 | |
| 25 | // Usage — pick the module, the creation is encapsulated |
| 26 | const module = new StripeModule(); |
| 27 | module.processPayment(100); |
Key difference: Simple Factory is just a function. Factory Method is an OOP pattern using inheritance where the base class has behaviour that depends on the object the subclass creates.
Scenario Quiz
?Knowledge Check
1You write a `createLogger('file')` function that returns FileLogger or ConsoleLogger based on the argument. No classes inherit from anything. Which pattern?
2A BaseExporter class has a `export()` method that calls `this.createWriter()`. CsvExporter and JsonExporter override createWriter(). Which pattern?
3A NestJS provider uses useFactory to return a CacheService based on an env variable. Which pattern?
4A plugin system where each plugin module extends BasePlugin and overrides createHandler() to provide a specific request handler. Which pattern?
5A config file maps 'email' -> EmailSender, 'sms' -> SmsSender. A factory function reads the config and returns the right sender. Which pattern?
6You want to add a new payment gateway. With Simple Factory, what do you change?
7You want to add a new payment gateway. With Factory Method, what do you change?
8When should you prefer Simple Factory over Factory Method?