Personal Object Assembly Line
In a nutshell, the Factory Design Pattern acts as your personal object assembly line, adapting to your needs and delivering the objects you need, right when you need them.
class Product:
def use(self):
raise NotImplementedError("Subclasses must implement the use() method")
class ConcreteProduct1(Product):
def use(self):
print("Using Product 1")
class ConcreteProduct2(Product):
def use(self):
print("Using Product 2")
class Factory:
@staticmethod
def create_product(product_type):
if product_type == "1":
return ConcreteProduct1()
elif product_type == "2":
return ConcreteProduct2()
else:
return None
# Client code
client = Factory.create_product("1") # Client doesn't know concrete product types
client.use() # Output: Using Product 1
client = Factory.create_product("2")
client.use() # Output: Using Product 2