/ Design Patterns / Creational patterns / Abstract Factory Abstract Factory Design Pattern in Python Back to Abstract Factory description """ Provide an interface for creating families of related or dependent objects without specifying their concrete classes. """ import abc class AbstractFactory(metaclass=abc.ABCMeta): """ Declare an interface for operations that create abstract product objects. """ @abc.abstractmethod def create_product_a(self): pass @abc.abstractmethod def create_product_b(self): pass class ConcreteFactory1(AbstractFactory): """ Implement the operations to create concrete product objects. """ def create_product_a(self): return ConcreteProductA1() def create_product_b(self): return ConcreteProductB1() class ConcreteFactory2(AbstractFactory): """ Implement the operations to create concrete product objects. """ def create_product_a(self): return ConcreteProductA2() def create_product_b(self): return ConcreteProductB2() class AbstractProductA(metaclass=abc.ABCMeta): """ Declare an interface for a type of product object. """ @abc.abstractmethod def interface_a(self): pass class ConcreteProductA1(AbstractProductA): """ Define a product object to be created by the corresponding concrete factory. Implement the AbstractProduct interface. """ def interface_a(self): pass class ConcreteProductA2(AbstractProductA): """ Define a product object to be created by the corresponding concrete factory. Implement the AbstractProduct interface. """ def interface_a(self): pass class AbstractProductB(metaclass=abc.ABCMeta): """ Declare an interface for a type of product object. """ @abc.abstractmethod def interface_b(self): pass class ConcreteProductB1(AbstractProductB): """ Define a product object to be created by the corresponding concrete factory. Implement the AbstractProduct interface. """ def interface_b(self): pass class ConcreteProductB2(AbstractProductB): """ Define a product object to be created by the corresponding concrete factory. Implement the AbstractProduct interface. """ def interface_b(self): pass def main(): for factory in (ConcreteFactory1(), ConcreteFactory2()): product_a = factory.create_product_a() product_b = factory.create_product_b() product_a.interface_a() product_b.interface_b() if __name__ == "__main__": main() Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...