Abstract Factory
Produce families of related objects without specifying their concrete classes.
Use it when
Your code must work with several families of related products, such as UI themes or database drivers.
/**
* Abstract Factory: one factory per UI theme, creating widgets that match.
*
* Real-world scenario: an app with light and dark themes. A login form needs a
* text field and a button, and both must come from the same theme. The form asks
* a theme factory for its widgets, so it never mixes a dark button with a light
* text field, and it never names a concrete widget class.
*/
// Abstract products: the widgets every theme provides.
interface Button {
render(label: string): string;
}
interface TextField {
render(placeholder: string): string;
}
// Concrete products: the light family.
class LightButton implements Button {
render(label: string): string {
return `[ ${label} ] (white background, dark text)`;
}
}
class LightTextField implements TextField {
render(placeholder: string): string {
return `|${placeholder.padEnd(20, "_")}| (white box, grey border)`;
}
}
// Concrete products: the dark family.
class DarkButton implements Button {
render(label: string): string {
return `[ ${label} ] (black background, light text)`;
}
}
class DarkTextField implements TextField {
render(placeholder: string): string {
return `|${placeholder.padEnd(20, "_")}| (charcoal box, no border)`;
}
}
// Abstract factory: one method per product in the family.
interface ThemeFactory {
createButton(): Button;
createTextField(): TextField;
}
// Concrete factories: each one creates a complete, matching family.
class LightTheme implements ThemeFactory {
createButton(): Button {
return new LightButton();
}
createTextField(): TextField {
return new LightTextField();
}
}
class DarkTheme implements ThemeFactory {
createButton(): Button {
return new DarkButton();
}
createTextField(): TextField {
return new DarkTextField();
}
}
// Client: works only with the abstract factory and abstract products.
function renderLoginForm(theme: ThemeFactory): void {
const email = theme.createTextField();
const login = theme.createButton();
console.log(email.render("Email"));
console.log(login.render("Log in"));
}
function clientCode(): void {
// The theme is chosen once, e.g. from the user's settings.
const themes: Record<string, ThemeFactory> = {
light: new LightTheme(),
dark: new DarkTheme(),
};
for (const setting of ["light", "dark"]) {
console.log(`--- ${setting} theme ---`);
renderLoginForm(themes[setting]);
}
}
clientCode();
// Output:
// --- light theme ---
// |Email_______________| (white box, grey border)
// [ Log in ] (white background, dark text)
// --- dark theme ---
// |Email_______________| (charcoal box, no border)
// [ Log in ] (black background, light text)How it works
| Role | Classes | What it does |
|---|---|---|
| Abstract products | Button, TextField | Interfaces for the widgets every theme provides |
| Concrete products | LightButton, LightTextField, DarkButton, DarkTextField | One matching family per theme |
| Abstract factory | ThemeFactory | Interface with one create method per widget |
| Concrete factories | LightTheme, DarkTheme | Each creates a complete, matching family |
| Client | renderLoginForm() | Builds the form from whichever theme it's given |