What is a factory pattern ?  Factory pattern is about encapsulating object creation. Factory is an object which specializes in creating other objects. The Factory pattern is useful when you’re not sure about what type of objects you’ll be needing at run time. For example in Data Science projects it is likely that there may be different data sources and you may want to abstract the data access functionality.

Factory pattern provides a pythonic way of achieving this.

Begin by importing the Abstract Base class and abstractmethod

from abc import ABC, abstractmethod

Then implement the abstract class containing the abstract functions which are needed for fetching the data. As a toy example, we have defined two functions, one for fetching one record and another for multiple records.

class DataFetcher(ABC):
    def fetch_one_record(self, recordid):
        """ Reads one data record identified by recordid """

    def fetch_n_records(self, recordids, max_result=-1, **kawrgs):
        """Reads n data records identified by a list of recordids"""

During run time you need to know the class name of the actual fetcher instance that needs to be created, for this you will need to implement a class function as below.

You need to make sure you apply it in the proper sequence where @abstractmethod appears immediately before the function definition, as shown here:

def fetcher_name(cls):

Finally, you will implement the factory function. The purpose of the  factory function,  is to create the objects and to return the objects to the user of the function. In this toy example, we are creating a new object using the argument passed to the factory. This is where the fetcher_name class function comes handy to identify the class that needs to be instantiated.

def data_fetcher_factory(fetcher='TextDataFetcher'):
    """ Factory for creating the data fetcher """
    for cls in DataFetcher.__subclasses__():
        if cls.fetcher_name()==fetcher:
            return cls()
    raise ValueError

During run time, you can involve data_fetcher_factory with the name of the fetcher type that you want to use.

You can later add a new fetchers by inheriting from the  DataFetcher abstract class and the data_fetcher_factory will be able to find it and create an instance at run time. I will share some examples of some fetcher implementations in future blogs.

If you have any questions or suggestions for future blogs, please do drop a line in the comments section.