Design Patterns

Pattern 3 of 23, Creational family

Abstract Factory

Also known as: Kit

Provide an interface for creating families of related objects without specifying their concrete classes.

The problem

Your UI has to run on macOS and Windows. Mixing a MacButton with a WinCheckbox gives an inconsistent interface, and if (os === ...) checks spread everywhere.

The solution

Declare a GUIFactory interface (createButton, createCheckbox…) and one concrete factory per family. The client receives a factory and creates its components only through it.

The pattern in action

  • Client
  • Key role of the pattern
  • Object
  • Data or state
  1. The application receives an abstract factory (here MacFactory).
  2. The factory creates a button from ITS OWN family.
  3. Then a checkbox.
  4. The products are guaranteed to be compatible: same family.
  5. Switching factories = switching the whole family, without touching the client code.

An analogy

A furniture store organized by style: pick "Scandinavian" and the chair, table and sofa all arrive matching.

When to use it

  • Several product families that must stay consistent
  • Themes, operating systems, cloud providers (AWS / GCP)
  • Isolate client code from concrete classes

Pros and cons

Pros

  • Guaranteed compatibility between products
  • Switch families in one line
  • Open/Closed for new families

Cons

  • Lots of interfaces and classes
  • Adding a new product type means changing every factory

Implementations

The same example, written idiomatically in each language.

Abstract Factory in Python

abstract-factory.py 42 lines
from abc import ABC, abstractmethod


class Button(ABC):
    @abstractmethod
    def render(self) -> str: ...

class Checkbox(ABC):
    @abstractmethod
    def render(self) -> str: ...


class MacButton(Button):
    def render(self): return "[ Mac Button ]"

class MacCheckbox(Checkbox):
    def render(self): return "[x] Mac"

class WinButton(Button):
    def render(self): return "< Win Button >"

class WinCheckbox(Checkbox):
    def render(self): return "(x) Win"


class GUIFactory(ABC):
    @abstractmethod
    def create_button(self) -> Button: ...
    @abstractmethod
    def create_checkbox(self) -> Checkbox: ...

class MacFactory(GUIFactory):
    def create_button(self): return MacButton()
    def create_checkbox(self): return MacCheckbox()

class WinFactory(GUIFactory):
    def create_button(self): return WinButton()
    def create_checkbox(self): return WinCheckbox()


def build_ui(factory: GUIFactory):
    return factory.create_button().render(), factory.create_checkbox().render()

Abstract Factory in TypeScript

abstract-factory.ts 23 lines
interface Button { render(): string }
interface Checkbox { render(): string }

interface GUIFactory {
  createButton(): Button;
  createCheckbox(): Checkbox;
}

class MacFactory implements GUIFactory {
  createButton() { return { render: () => "[ Mac Button ]" }; }
  createCheckbox() { return { render: () => "[x] Mac" }; }
}

class WinFactory implements GUIFactory {
  createButton() { return { render: () => "< Win Button >" }; }
  createCheckbox() { return { render: () => "(x) Win" }; }
}

function buildUI(f: GUIFactory) {
  return [f.createButton().render(), f.createCheckbox().render()];
}

buildUI(process.platform === "darwin" ? new MacFactory() : new WinFactory());

Abstract Factory in Java

abstract-factory.java 27 lines
interface Button { String render(); }
interface Checkbox { String render(); }

interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

class MacFactory implements GUIFactory {
    public Button createButton()     { return () -> "[ Mac Button ]"; }
    public Checkbox createCheckbox() { return () -> "[x] Mac"; }
}

class WinFactory implements GUIFactory {
    public Button createButton()     { return () -> "< Win Button >"; }
    public Checkbox createCheckbox() { return () -> "(x) Win"; }
}

class App {
    private final Button button;
    private final Checkbox checkbox;

    App(GUIFactory factory) {
        button = factory.createButton();
        checkbox = factory.createCheckbox();
    }
}

Abstract Factory in C#

abstract-factory.cs 30 lines
interface IButton { string Render(); }
interface ICheckbox { string Render(); }

interface IGuiFactory
{
    IButton CreateButton();
    ICheckbox CreateCheckbox();
}

class MacButton : IButton { public string Render() => "[ Mac Button ]"; }
class MacCheckbox : ICheckbox { public string Render() => "[x] Mac"; }
class WinButton : IButton { public string Render() => "< Win Button >"; }
class WinCheckbox : ICheckbox { public string Render() => "(x) Win"; }

class MacFactory : IGuiFactory
{
    public IButton CreateButton() => new MacButton();
    public ICheckbox CreateCheckbox() => new MacCheckbox();
}

class WinFactory : IGuiFactory
{
    public IButton CreateButton() => new WinButton();
    public ICheckbox CreateCheckbox() => new WinCheckbox();
}

class App(IGuiFactory f)
{
    public string Draw() => f.CreateButton().Render() + " " + f.CreateCheckbox().Render();
}

Abstract Factory in PHP

abstract-factory.php 23 lines
interface Button { public function render(): string; }
interface Checkbox { public function render(): string; }

interface GUIFactory
{
    public function createButton(): Button;
    public function createCheckbox(): Checkbox;
}

class MacButton implements Button { public function render(): string { return '[ Mac Button ]'; } }
class MacCheckbox implements Checkbox { public function render(): string { return '[x] Mac'; } }

class MacFactory implements GUIFactory
{
    public function createButton(): Button { return new MacButton(); }
    public function createCheckbox(): Checkbox { return new MacCheckbox(); }
}
// WinFactory: same structure with WinButton / WinCheckbox

function buildUI(GUIFactory $f): array
{
    return [$f->createButton()->render(), $f->createCheckbox()->render()];
}

Abstract Factory in Go

abstract-factory.go 26 lines
type Button interface{ Render() string }
type Checkbox interface{ Render() string }

type GUIFactory interface {
    CreateButton() Button
    CreateCheckbox() Checkbox
}

type macButton struct{}

func (macButton) Render() string { return "[ Mac Button ]" }

type macCheckbox struct{}

func (macCheckbox) Render() string { return "[x] Mac" }

type MacFactory struct{}

func (MacFactory) CreateButton() Button     { return macButton{} }
func (MacFactory) CreateCheckbox() Checkbox { return macCheckbox{} }

// WinFactory: same structure

func BuildUI(f GUIFactory) (string, string) {
    return f.CreateButton().Render(), f.CreateCheckbox().Render()
}

Frequently asked questions

What is the Abstract Factory design pattern?

Provide an interface for creating families of related objects without specifying their concrete classes. Declare a GUIFactory interface (createButton, createCheckbox…) and one concrete factory per family. The client receives a factory and creates its components only through it.

When should you use the Abstract Factory pattern?

Several product families that must stay consistent; Themes, operating systems, cloud providers (AWS / GCP); Isolate client code from concrete classes.

What are the pros and cons of the Abstract Factory pattern?

Pros: Guaranteed compatibility between products; Switch families in one line; Open/Closed for new families. Cons: Lots of interfaces and classes; Adding a new product type means changing every factory.