CREATIONAL PATTERNS / OBJECT DESIGN BRIEF

Abstract Factory pattern

Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes.

IntermediatePhase 04 / Topic 2 of 6ResponsibilitiesCollaborationsExtensibility
01

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.

Furniture collections

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.

02

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.
03

Where it shows up in interviews

Families of products

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
04

Where it is used in real software

Java AWT/Swing look and feel

UIManager swaps an entire family of component renderers.

JDBC

A Connection acts as a factory for matching Statement and PreparedStatement objects from the same driver.

Cloud abstraction layers

Libraries like jclouds provide provider-specific families of storage and compute clients.

05

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.
06

How it works, step by step

  1. 1
    List product types

    Button, Checkbox, Dialog.

  2. 2
    Define product interfaces

    One per type.

  3. 3
    Define the factory interface

    createButton(), createCheckbox().

  4. 4
    Implement a factory per family

    LightThemeFactory, DarkThemeFactory.

  5. 5
    Choose the factory once

    At the composition root; pass it to clients.

07

Theme factories

Clients call factory.createButton() and factory.createDialog()

Step 1 / 3
FactorycreateButton()createDialog()
LightFactoryLightButtonLightDialog
DarkFactoryDarkButtonDarkDialog
HighContrastFactory (new)HighContrastButtonHighContrastDialog

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).

08

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());
09

Complexity and performance

New family1 factory + n products

No client changes.

New product typeTouches every factory

Main cost.

10

Trade-offs

Consistency vs rigidity

Guarantees compatible products but adding a product type changes every factory.

More classes

Overkill when there is only one family or one product.

11

Variants and related techniques

Factory registry

Map of family name to factory, chosen by configuration.

Dependency injection modules

DI modules often act as abstract factories.

12

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.

13

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.

14

Practice problems

ProblemDifficultyWhat it trains
Cross-platform UI with Windows and Mac familiesMediumProduct families.
Payment provider families (charge, refund, webhook parser)MediumConsistency.