Python ProgrammingPython Programming

Implementation of the Abstract Factory Pattern

The Abstract Factory design pattern is implemented as a number of Factory Methods that belong to a single class and are used to create a family of related objects (various gadgets, smartphones, smartwatch, and so forth). Develop a program to demonstrate the use of abstract factory design pattern.
# Python program with implementation of the Abstract Factory pattern

from abc import ABCMeta, abstractmethod

class Gadget(metaclass=ABCMeta):
    def sellSmartPhone(self):

    def sellSmartWatch(self):

class Samsung(Gadget):
    def sellSmartPhone(self):
        return GalaxySeries()

    def sellSmartWatch(self):
        return SamsungGearSeries()

class Apple(Gadget):
    def sellSmartPhone(self):
        return IphoneSeries()

    def sellSmartWatch(self):
        return AppleWatchSeries()

class SmartPhone(metaclass=ABCMeta):
    def callingGadget(self, SmartPhone):

class SmartWatch(metaclass=ABCMeta):
    def wearableGadget(self, SmartWatch):

class GalaxySeries(SmartPhone):
    def callingGadget(self):
        print("Samsung Galaxy S8 Plus (Midnight Black, 64 GB) (4 GB RAM)\n"
              "Samsung Galaxy S7 Edge (Black Onyx, 32 GB) (4 GB RAM)\n"
              "Samsung Galaxy Note 5 64GB Single Sim-Gold(Gold, 64GB)")

class IphoneSeries(SmartPhone):
    def callingGadget(self):
        print("Apple iPhone 7 (Jet Black, 128 GB)\n"
              "Apple iPhone SE (Space Grey, 16 GB)\n"
              "Apple iPhone 5s (Silver, 16 GB)")

class SamsungGearSeries(SmartWatch):
    def wearableGadget(self):
        print("Samsung Gear S2 Silver Smartwatch  (Silver Strap Regular)\n"
              "Samsung Gear S2 Classic Platinum Smartwatch\n")

class AppleWatchSeries(SmartWatch):
    def wearableGadget(self):
        print("Apple Watch Series 2 - 42 mm Rose Gold Case with \
        Midnight Blue Sports Band  (Blue Strap Medium)")

class GadgetStore:
    def __init__(self):

    def storeGadgets(self):
        for store in [Apple(), Samsung()]:
   = store
            self.SmartPhone =
            self.SmartWatch =

def main():
    account = GadgetStore()


There is a abstract base class Gadget, the Gadget class has two abstract methods, sellSmartPhone() and sellSmartWatch(). In this example, there are two concrete factories, namely Samsung and Apple. The above code have two abstract classes SmartPhone and SmartWatch define as (AbstractProduct) have individually defined callingGadget and wearableGadget methods.
Sample output of above program.
C:\Python\programs\designpatterns>pep8 --first

Apple iPhone 7 (Jet Black, 128 GB)
Apple iPhone SE (Space Grey, 16 GB)
Apple iPhone 5s (Silver, 16 GB)
Apple Watch Series 2 - 42 mm Rose Gold Case with Midnight Blue Sports Band (Blue Strap Medium)
Samsung Galaxy S8 Plus (Midnight Black, 64 GB) (4 GB RAM)
Samsung Galaxy S7 Edge (Black Onyx, 32 GB) (4 GB RAM)
Samsung Galaxy Note 5 64GB Single Sim-Gold(Gold, 64GB)
Samsung Gear S2 Silver Smartwatch (Silver Strap Regular)
Samsung Gear S2 Classic Platinum Smartwatch