design patterns · high
Abstract Factory Pattern
Abstract Factory is a creational GoF pattern that provides an interface for creating families of related objects without specifying concrete classes, while guaranteeing the family stays consistent. Classic example: a cross-platform UI where a Button and Checkbox must both be Windows-themed or both Mac-themed — never mixed. The abstract factory (GuiFactory) declares one creation method per product (makeButton, makeCheckbox); each concrete factory (WinFactory, MacFactory) implements all of them to produce one coherent family. The client depends only on the abstract factory and abstract products, so selecting the concrete factory once at the composition root (by OS, config, or tenant) switches the whole product line with no client changes; a map of factories makes this data-driven. Pros: family consistency, isolation of concrete classes (dependency inversion), trivial product-line swaps. Con: adding a NEW product type to the family forces changes to the factory interface and every concrete factory (open to new families, closed to new products). Use it for stable families that vary together (cross-platform UI, per-vendor DB driver suites of Connection+Command+Reader, export-format sets); for a single product, use Factory Method instead.
Abstract Factory (creational): an interface to create FAMILIES of related objects (Button+Checkbox per platform) without naming concrete classes, guaranteeing the family stays consistent. One make-method per product; each concrete factory builds one matching family; pick the factory at the composition root. Strong for adding new families, weak for adding new products (touches every factory). Factory Method = one product; Abstract Factory = a family.
The code
// Abstract Factory — create FAMILIES of related objects without// naming their concrete classes; keep a family consistent.struct Button { virtual void paint() = 0; virtual ~Button() = default;};struct Checkbox { virtual void paint() = 0; virtual ~Checkbox() = default;};
// The abstract factory: one method per product in the family.struct GuiFactory { virtual std::unique_ptr<Button> makeButton() = 0; virtual std::unique_ptr<Checkbox> makeCheckbox() = 0; virtual ~GuiFactory() = default;};
// Concrete factory #1 — the WINDOWS family (all parts match).struct WinFactory : GuiFactory { std::unique_ptr<Button> makeButton() override { return std::make_unique<WinButton>(); } std::unique_ptr<Checkbox> makeCheckbox() override { return std::make_unique<WinCheckbox>(); }};// MacFactory makes MacButton + MacCheckbox, etc.
void buildUI(GuiFactory& f) { // client: no concrete names auto b = f.makeButton(); auto c = f.makeCheckbox(); b->paint(); c->paint(); // guaranteed same theme}What this lesson walks through
- 01Intent — create whole FAMILIES that match
- 02Structure — one factory iface, many products
- 03Pick a family once; every product fits
- 04Gotcha — adding a product touches every factory
Abstract Factory creates families of related objects that must be used together — a Windows button with a Windows checkbox, never a Mac button with a Windows checkbox. The client gets a consistent set without naming concrete classes.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of Abstract Factory Pattern and 100+ animated C++ interview lessons.