The Factory Pattern puts object creation in one place. Code that needs an object asks the factory for one by name, and the factory decides which class to build, so the rest of your program never has to know.
The problem a factory solves
Say a cat toy app lets the user pick a ball, a mouse or a feather. The obvious way to build the chosen toy is to check the choice and call the matching class:
if choice == "ball":
toy = Ball()
elif choice == "mouse":
toy = Mouse()
elif choice == "feather":
toy = Feather()That works once. The trouble starts when the same block turns up in the shop page, the gift screen and the tests. Add a laser pointer and you have to find and edit every copy. Miss one, and that screen can't make the new toy.
This is what the video means by hard-coding object creation everywhere. Each caller knows every concrete class and how to construct it, so a change to any of them ripples through the whole codebase.
How a factory works
A factory moves that decision into one function (or class). Callers hand it a description of what they want, and it hands back a ready-to-use object.
Wondering how this was made? How we make these
