Section 1/4The Problem Without Factory
The Problem Without Factory
If you add a new animal type, you must find and update every place that has this if/elif chain. The Factory centralises this logic.
Extension: Factory Pattern
The Factory pattern provides a way to create objects without specifying their exact class at the call site. You ask a factory for an object, and the factory decides which class to instantiate.
The Problem Without Factory
# Without factory — caller must know every concrete class
animal_type = get_user_input() # "dog" or "cat"
if animal_type == "dog": animal = Dog()
elif animal_type == "cat": animal = Cat()
# This if/elif must be repeated everywhere...If you add a new animal type, you must find and update every place that has this if/elif chain. The Factory centralises this logic.