Before you start
Welcome to design patterns. If you take one thing from this module, take this:
Knowing patterns is not the interview skill. Choosing the right one for a specific problem is.
Almost every candidate can list Factory / Strategy / Observer and describe what they do. Almost none can look at a new problem, ask three good questions, and say "Factory here because of X, not Strategy because of Y". That's the gap this module drills.
A checklist before you use any pattern
Walk this checklist BEFORE reaching for a pattern:
- What does the problem actually need? List what varies. (In our parking lot: the specific class varies based on an input code.)
- What's the simplest thing that could work? Start there. Only make it fancier if the simple thing hurts.
- What are 2-3 different designs? For each, know the good parts, the bad parts, and the exact requirement that makes it win or lose.
- Which requirement makes the choice? Say it out loud: "I'm picking X because the requirements say Y."
Skip step 4 and the interviewer will ask "why not Z?" and you won't have an answer. That's not a design failure — that's a communication failure. The choice was in your head; you didn't SHOW them how you got there.
When to use a factory (any flavor)
The factory family answers one question: how do we hide the "which class do I build?" decision from the caller?
Signs you need one:
- The class you build depends on data at runtime. The caller has a string / config / DB row, not a class name.
- Callers shouldn't import specific classes. They should
hold a
Vehicle, not aCar/Truck/Motorcycle. - New types will be added later. Callers shouldn't have to change when new types show up.
- Building the object has cross-cutting logic (logging, metrics, auth check) that shouldn't be copied everywhere.
Any of these → some kind of factory. Which specific flavor depends on the answers to the questions in the objective.
The factory flavors, at a glance
| Flavor | Best when | Not when |
|---|---|---|
Inline if/elif |
1 place builds it, 2-3 types forever | Anything else |
| Dict + plain function | All types take the same args, few callers | Args differ per type |
| Simple Factory class (this lesson) | Args differ per type, want one place for cross-cutting logic, list will grow | Only ever 1 type |
| GoF Factory Method (child overrides builder) | Parallel class trees where each child owns a whole workflow AND builds a type-specific object | Simple "just build the right thing" — this is overkill |
| Abstract Factory (next lesson) | Need to build FAMILIES of related objects together (like a whole UI theme) | Only one thing to build |
For interviews, the top three rows cover 95% of "give me a factory" questions.
Where you'll see Factory later in this course
- Parking Lot — VehicleFactory (this lesson's task)
- Vending Machine — ProductFactory (Snack / Beverage / ColdItem based on the slot type)
- Chess — PieceFactory from FEN notation ("Q" → Queen, "k" → BlackKing)
- Notification — NotifierFactory based on user preferences
- File System — NodeFactory that returns File or Directory based on what's on disk
Every problem where "the type comes from data" → factory.
Common mistakes to avoid
Building a factory for one type. If there's only one class, just call
Car(plate). A factory for one type is extra work without payoff.Putting
if/elifinside the factory. If yourcreate()containsif type_code == "CAR": return Car(...), you've just moved the mess into a new building. The whole POINT of the registry is to remove the if/elif. The test on this lesson catches this.Confusing "Simple Factory" with GoF "Factory Method". Simple Factory is a class or function that returns the right instance. GoF Factory Method is a parent-class hook that children override to build a type-specific object as part of a shared workflow. Different patterns. Different when-to-use. Interviewers who know the GoF book will notice if you mix them up.
What the tests in this lesson check
Three things:
- Correctness — the factory returns the right subclasses.
- Extensibility — a new type registered from outside the factory must work (the OCP payoff).
- Anti-smell — the AST check that if/elif never sneaks back in.
That third one is where design quality lives. Pass all three and you have a factory design an interviewer would sign off on.