Overview
Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. A UIFactory creates a Button, a Checkbox, and a Dialog that all match the same theme or platform. The client picks one factory, and every product it creates is guaranteed to be compatible.
Factory Method creates one product and lets subclasses decide which; Abstract Factory groups several factory methods so a whole family changes together. It shines when mixing products from different families would be a bug: a dark button with a light dialog, or an AWS queue with an Azure blob client.
An IKEA collection gives you a matching chair, table, and sofa. You choose 'Modern' or 'Victorian' once and every piece fits together. You never get a Victorian chair with a modern table by accident.
When to use it
- Several related objects must be created consistently (theme, platform, vendor).
- The family is chosen once at startup or per tenant.
- Clients should not know concrete classes.
Where it shows up in interviews
Recognize it when: multiple platforms, themes, or vendors with several components each.
- Design a cross-platform UI toolkit
- Design a multi-cloud storage and queue client
Where it is used in real software
UIManager swaps an entire family of component renderers.
A Connection acts as a factory for matching Statement and PreparedStatement objects from the same driver.
Libraries like jclouds provide provider-specific families of storage and compute clients.
Key terms
- Abstract factory
- Interface with create methods for each product.
- Concrete factory
- Implements all create methods for one family.
- Product family
- Objects designed to work together.
- Abstract product
- Interface for each product type.
How it works, step by step
- 1List product types
Button, Checkbox, Dialog.
- 2Define product interfaces
One per type.
- 3Define the factory interface
createButton(), createCheckbox().
- 4Implement a factory per family
LightThemeFactory, DarkThemeFactory.
- 5Choose the factory once
At the composition root; pass it to clients.
Theme factories
Clients call factory.createButton() and factory.createDialog()
| Factory | createButton() | createDialog() |
|---|---|---|
| LightFactory | LightButton | LightDialog |
| DarkFactory | DarkButton | DarkDialog |
| HighContrastFactory (new) | HighContrastButton | HighContrastDialog |
NOWFactory: LightFactory | createButton(): LightButton | createDialog(): LightDialog
Adding a family is a new factory; adding a new product type requires changing every factory (the pattern's main trade-off).
Implementation
interface Button { render(): string }interface Dialog { render(body: string): string } interface UIFactory { createButton(label: string): Button; createDialog(): Dialog;} class DarkFactory implements UIFactory { createButton(label: string): Button { return { render: () => `[dark-btn ${label}]` }; } createDialog(): Dialog { return { render: (b) => `<dark-dialog>${b}</dark-dialog>` }; }} class LightFactory implements UIFactory { createButton(label: string): Button { return { render: () => `[light-btn ${label}]` }; } createDialog(): Dialog { return { render: (b) => `<light-dialog>${b}</light-dialog>` }; }} function confirmScreen(ui: UIFactory) { return ui.createDialog().render(ui.createButton("OK").render()); // always consistent} confirmScreen(prefersDark ? new DarkFactory() : new LightFactory());Complexity and performance
No client changes.
Main cost.
Trade-offs
Guarantees compatible products but adding a product type changes every factory.
Overkill when there is only one family or one product.
Variants and related techniques
Map of family name to factory, chosen by configuration.
DI modules often act as abstract factories.
Common mistakes
- Using it for a single product.
Fix: Use Factory Method or a simple function.
- Clients still instantiating some concrete products.
Fix: Route all creation through the factory to keep families consistent.
Interview questions
Factory Method vs Abstract Factory?
Factory Method defines one creation method that subclasses override to create one product. Abstract Factory is an object with several creation methods producing a family of related products that must be used together.
What is the main drawback of Abstract Factory?
Adding a new kind of product requires changing the factory interface and every concrete factory.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Cross-platform UI with Windows and Mac families | Medium | Product families. |
| Payment provider families (charge, refund, webhook parser) | Medium | Consistency. |