Lesson 1 · Unit 02
Factories
One function decides which class to build
What you practise
- A function that chooses the type, a registry keyed by name, parsing input into the right object, when a plain constructor is enough
Try a question from this lesson
Pick one
A third format arrives: files ending in .json. Callers already ask the factory for a reader. Where does the choosing code change?
Correct answer
Only inside the factory function
Why
Callers never chose a class, so they have nothing to update. One function made the decision, and one function learns about the new case.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Factory
A factory is a function that decides which class to build and returns the finished object.
Callers say what they need ("a reader for this file") and never name a class. The knowledge of which classes exist, and how to choose between them, sits in one place.
9 more exercises in the app
- 2× type the answer
- 1× fill the blanks
- 3× multiple choice
- 1× true or false
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.