Python Abstract Factory Pattern Example

In this article, we will learn how to use and implement the Abstract Factory Pattern in Python with an example.

Abstract factory pattern has a creational purpose and provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Python Abstract Factory Pattern Example

The below diagram shows the generic structure of the Abstract Factory Pattern:
Let's refer to the above structure to create an example to demonstrates the usage of the Abstract Factory Pattern in Python.

The following program helps in implementing the abstract factory pattern. The comments in the program are self-descriptive to understand the abstract factory pattern implementation.

import sys


#
# Product A
# products implement the same interface so that the classes can refer
# to the interface not the concrete product
#

class ProductA:

    def getName(self):
        pass


#
# ConcreteProductAX and ConcreteProductAY
# define objects to be created by concrete factory
#

class ConcreteProductAX(ProductA):

    def getName(self):
        return 'A-X'


class ConcreteProductAY(ProductA):

    def getName(self):
        return 'A-Y'


#
# Product B
# same as Product A, Product B declares interface for concrete products
# where each can produce an entire set of products
#

class ProductB:

    def getName(self):
        pass


#
# ConcreteProductBX and ConcreteProductBY
# same as previous concrete product classes
#

class ConcreteProductBX(ProductB):

    def getName(self):
        return 'B-X'


class ConcreteProductBY(ProductB):

    def getName(self):
        return 'B-Y'


#
# Abstract Factory
# provides an interface for creating a family of products
#

class AbstractFactory:

    def createProductA(self):
        pass

    def createProductB(self):
        pass


#
# Concrete Factories
# each concrete factory create a family of products and
# client uses one of these factories
#

class ConcreteFactoryX(AbstractFactory):

    def createProductA(self):
        return ConcreteProductAX()

    def createProductB(self):
        return ConcreteProductBX()


class ConcreteFactoryY(AbstractFactory):

    def createProductA(self):
        return ConcreteProductAY()

    def createProductB(self):
        return ConcreteProductBY()


if __name__ == '__main__':
    factoryX = ConcreteFactoryX()
    factoryY = ConcreteFactoryY()

    p1 = factoryX.createProductA()
    print 'Product: ' + p1.getName()

    p2 = factoryY.createProductA()
    print 'Product: ' + p2.getName()

Output

The above program generates the following output:

Product: A-X
Product: A-Y

Abstract Factory Design Pattern Benefits

  • Abstract Factory design pattern provides an approach to code for interface rather than implementation.
  • Abstract Factory pattern is “factory of factories” and can be easily extended to accommodate more products, 
  • Abstract Factory pattern is robust and avoids the conditional logic of the Factory pattern.

When to Use?

  • a system should be independent of how its products are created, composed, and represented
  • a system should be configured with one of multiple families of products
  • a family of related product objects is designed to be used together
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations

Comments