Abstract factory
medium 50 minThe 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
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
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
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
criticalThe client depends on Windows through the factory.
Wrongclass GUIFactory(ABC): def create_button(self) -> WindowsButton: ...Rightclass GUIFactory(ABC): def create_button(self) -> Button: ...Two factories in one client
criticalThe families can be mixed again, and nothing notices.
Wrongclass Window: def __init__(self, buttons, checkboxes): self.b = buttons.create_button() self.c = checkboxes.create_checkbox()Rightclass Window: def __init__(self, factory): self.b = factory.create_button() self.c = factory.create_checkbox()A quiet default
majorA typo picks a family that nobody wanted.
Wrongreturn FAMILIES.get(name, WindowsFactory)()Rightif name not in FAMILIES: raise ValueError(name)return FAMILIES[name]()Asking the product for its family
majorThe condition that you removed is back in the client.
Wrongif isinstance(button, MacButton): margin = 12Rightmargin = button.margin()State in the factory
majorAll windows share it.
Wrongclass MacFactory: def create_checkbox(self): return self._checkbox # always the sameRightclass 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.