Factory Method
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Use it when
A class can't anticipate the exact type of objects it must create.
/**
* Factory Method: each card issuer decides which credit card it creates.
*
* Real-world scenario: a bank's card onboarding service. The approval workflow
* (eligibility check, card summary) is the same for every product, so it lives
* once in CardIssuer. Each issuer subclass overrides createCard() to decide
* which card to make. Launching a new product means adding a card class and an
* issuer class; the workflow and the client code don't change.
*/
// Product: what every credit card can do.
interface CreditCard {
readonly name: string;
readonly creditLimit: number;
readonly annualFee: number;
readonly minIncome: number;
rewardPoints(spend: number): number;
}
// Concrete products.
class ClassicCard implements CreditCard {
readonly name = "Classic Card";
readonly creditLimit = 5_000;
readonly annualFee = 0;
readonly minIncome = 20_000;
rewardPoints(spend: number): number {
return Math.floor(spend); // 1 point per $1
}
}
class PlatinumCard implements CreditCard {
readonly name = "Platinum Card";
readonly creditLimit = 25_000;
readonly annualFee = 250;
readonly minIncome = 80_000;
rewardPoints(spend: number): number {
return Math.floor(spend * 3); // 3 points per $1
}
}
const usd = (amount: number): string => `$${amount.toLocaleString("en-US")}`;
// Creator: owns the workflow and calls the factory method.
abstract class CardIssuer {
// Factory method: subclasses decide which card to create.
protected abstract createCard(): CreditCard;
issueCard(customer: string, annualIncome: number): string {
const card = this.createCard();
if (annualIncome < card.minIncome) {
return (
`DECLINED ${customer}: ${card.name} needs ${usd(card.minIncome)} income, ` +
`has ${usd(annualIncome)}`
);
}
const points = card.rewardPoints(1_000).toLocaleString("en-US");
return (
`APPROVED ${customer}: ${card.name}, limit ${usd(card.creditLimit)}, ` +
`fee ${usd(card.annualFee)}/yr, ${points} pts per $1,000`
);
}
}
// Concrete creators: each one overrides only the factory method.
class ClassicCardIssuer extends CardIssuer {
protected createCard(): CreditCard {
return new ClassicCard();
}
}
class PlatinumCardIssuer extends CardIssuer {
protected createCard(): CreditCard {
return new PlatinumCard();
}
}
function processApplication(issuer: CardIssuer, customer: string, income: number): void {
// Client code only knows the CardIssuer interface, never a concrete card.
console.log(issuer.issueCard(customer, income));
}
function clientCode(): void {
// Applications from the website: the product picked on the form selects
// the issuer. A new product is one new entry here plus its two classes.
const issuers: Record<string, CardIssuer> = {
classic: new ClassicCardIssuer(),
platinum: new PlatinumCardIssuer(),
};
const applications: Array<[string, string, number]> = [
["Asha", "classic", 32_000],
["Ravi", "platinum", 120_000],
["Meena", "platinum", 45_000],
];
for (const [customer, product, income] of applications) {
processApplication(issuers[product], customer, income);
}
}
clientCode();
// Output:
// APPROVED Asha: Classic Card, limit $5,000, fee $0/yr, 1,000 pts per $1,000
// APPROVED Ravi: Platinum Card, limit $25,000, fee $250/yr, 3,000 pts per $1,000
// DECLINED Meena: Platinum Card needs $80,000 income, has $45,000How it works
| Role | Classes | What it does |
|---|---|---|
| Product | CreditCard | Interface for every card: limit, fee and rewardPoints() |
| Concrete products | ClassicCard, PlatinumCard | Different limits, fees and reward rates |
| Creator | CardIssuer | Runs the shared issueCard() workflow and declares abstract createCard() |
| Concrete creators | ClassicCardIssuer, PlatinumCardIssuer | Each overrides only createCard() |
| Client | processApplication() | Works with any CardIssuer, never a concrete card |