The Factory Method Pattern defines an interface for creating an object but allows subclasses to decide which class to instantiate. This approach lets a class defer instantiation to subclasses, enhancing flexibility and encapsulation.
- Useful when there is a need to manage and maintain a large collection of smaller objects that share properties.
- Often used in applications where management of database connections and their allocation is needed.
The factory_method.py file contains a Python class that demonstrates the implementation. This pattern uses a creator class with a default implementation of the factory method that returns a default object. It also allows subclasses to override this method to return different types of objects.
creator = ConcreteCreator()
product = creator.factory_method()
print(product.operation())creator = ConcreteCreator()
product = creator.factory_method()
print(product.operation())Result of the ConcreteProduct operation.This output shows how the product created by the factory method performs its operation, demonstrating the dynamic creation of objects.
Here is how you can use the Factory Method class to perform some business logic:
singleton = Singleton()
singleton.some_business_logic()The test_factory_method.py file includes tests to ensure that the Factory Method pattern is correctly implemented and that it can dynamically create various types of product instances.