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):
    @abstractmethod
    def sellSmartPhone(self):
        pass

    @abstractmethod
    def sellSmartWatch(self):
        pass


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):
    @abstractmethod
    def callingGadget(self, SmartPhone):
        pass


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


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):
        pass

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


def main():
    account = GadgetStore()
    account.storeGadgets()

main()

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 factory2.py

C:\Python\programs\designpatterns>python factory2.py
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


C:\Python\programs\designpatterns>