AlgoViz

Abstract factory

medium 50 min

The Abstract factory creates families of objects that must fit together, without the client knowing a single concrete class.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:44

See it run

One factory object, one family. Watch who creates what, and what the client never learns.

Operations

Furniture showroom

A room is furnished from the Modern factory, then from the Victorian one.

Step 1 of 7

Select a style

roomRoomchairnonetablenonesofanonefactoryModernFactory

The customer chose Modern, so the room holds a ModernFurnitureFactory.

Step 1: Select a style

class Room:    def __init__(self, factory):        self.chair = factory.create_chair()        self.table = factory.create_table()        self.sofa = factory.create_sofa()class ModernFurnitureFactory:    def create_chair(self): return ModernChair()    def create_table(self): return ModernTable()    def create_sofa(self): return ModernSofa()class VictorianFurnitureFactory:    def create_chair(self): return VictorianChair()    def create_table(self): return VictorianTable()    def create_sofa(self): return VictorianSofa()
Watch
The classes on the right
Result
Always one family

The left side sends the same calls in both rounds.

In the real world

The same abstract factory doing real jobs. Pick one and step through it.

Two tenants on the same cloud provider.

Step 1 of 3

Tenant acme

requestRequestregistryRegistryawsacme'saws1AwsFactoryaccountacmefor_tenant(acme)AwsFactory

The registry makes an AWS factory and stores it under the family name.

Step 1: Tenant acme

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Concrete return types

    critical

    The client depends on Windows through the factory.

    Wrong
    class GUIFactory(ABC):    def create_button(self) -> WindowsButton: ...
    Right
    class GUIFactory(ABC):    def create_button(self) -> Button: ...
  • Two factories in one client

    critical

    The families can be mixed again, and nothing notices.

    Wrong
    class Window:    def __init__(self, buttons, checkboxes):        self.b = buttons.create_button()        self.c = checkboxes.create_checkbox()
    Right
    class Window:    def __init__(self, factory):        self.b = factory.create_button()        self.c = factory.create_checkbox()
  • A quiet default

    major

    A typo picks a family that nobody wanted.

    Wrong
    return FAMILIES.get(name, WindowsFactory)()
    Right
    if name not in FAMILIES:    raise ValueError(name)return FAMILIES[name]()
  • Asking the product for its family

    major

    The condition that you removed is back in the client.

    Wrong
    if isinstance(button, MacButton):    margin = 12
    Right
    margin = button.margin()
  • State in the factory

    major

    All windows share it.

    Wrong
    class MacFactory:    def create_checkbox(self):        return self._checkbox      # always the same
    Right
    class MacFactory:    def create_checkbox(self):        return MacCheckbox()

Interview tips

  • Draw the grid

    • Kinds across.
    • Families down.
    • Must the rows match?
  • Keep names in one place

    • Concrete names in their factory.
    • Factories in one map.
    • Interfaces everywhere else.
  • Say both halves

    • New family: easy.
    • New kind: every factory.
    • So list the kinds early.
  • Protect the family

    • One factory per client.
    • One contract test.
    • Owner check if mixing costs money.
  • Stay small

    • Values: a record.
    • Short creation: functions.
    • State or outsiders: classes.