Abstract Factory

A factory is a fancy term given to a routine that creates and returns new objects. For example,

class FrenchTranslator:
    def translate(self, word):
        return self.mappings[word]
 
def french_translator():
    mappings = {}
    return FrenchTranslator(mappings)
 
def main():
    print(french_translator().translate("car"))

Here french_translator is a factory. There can be other translators as well.

An abstract factory is a abstract class that defines interfaces that it’s implemented factory should have. These interfaces are factories or functions that create objects.

class TranslatorFactory(metaclass=ABCMeta):
    @abstracmethod
    def get_translator():
        pass
 
class FrenchTranslatorFactory(translatorFactory):
    @staticmethod
    def get_translator():
        mappings = {}
        return FrenchTranslator(mappings)

References

  1. https://python-patterns.guide/gang-of-four/abstract-factory/