Python ProgrammingPython Programming

Factory method implementation

An example of the Factory Method pattern used in gadget features list.(The Factory Method centralizes an object creation and tracking your objects becomes much easier. The Factory method creation is through inheritance and not through instantiation.)
from abc import ABCMeta, abstractmethod


class Feature(metaclass=ABCMeta):
    @abstractmethod
    def specifications(self):
        pass


class DisplayFeatures(Feature):
    def specifications(self):
        print("5.5 inch, 1280 x 720 Pixels, TFT LCD IPS")


class ProcessorFeatures(Feature):
    def specifications(self):
        print("MediaTek MTK6737 1.3GHz, Quad Core, 1.3 GHz")


class StorageFeatures(Feature):
    def specifications(self):
        print("Internal Storage 32GB, RAM 3GB")


class CallFeatures(Feature):
    def specifications(self):
        print("Voice Call, Phonebook")


class Gadgets(metaclass=ABCMeta):
    def __init__(self):
        self.features = []
        self.featureList()

    @abstractmethod
    def featureList(self):
        pass

    def getFeatures(self):
        return self.features

    def addFeatures(self, feature):
        self.features.append(feature)


class Mobile(Gadgets):
    def featureList(self):
        self.addFeatures(DisplayFeatures())
        self.addFeatures(ProcessorFeatures())
        self.addFeatures(CallFeatures())
        self.addFeatures(StorageFeatures())


class Tablet(Gadgets):
    def featureList(self):
        self.addFeatures(DisplayFeatures())
        self.addFeatures(StorageFeatures())
        self.addFeatures(ProcessorFeatures())


def main():
    print("##### MOBILE FEATURE LIST #####")
    gadget1 = Mobile()
    for x in gadget1.getFeatures():
        x.specifications()

    print('\n')
    print("##### TABLET FEATURE LIST #####")
    gadget2 = Tablet()
    for y in gadget2.getFeatures():
        y.specifications()

main()


In above program a Feature abstract class that defines how a feature will be. To keep it very simple added an abstract method, specifications(). There are multiple Concrete classes, DisplayFeatures, ProcessorFeatures, StorageFeatures and CallFeatures. These classes implement the specifications() abstract method and print their respective features. The Creator abstract class that is named Gadgets. The Gadgets [Creator] abstract class provides a factory method, featureList(). The featureList() method should be implemented by ConcreteClass to actually create the gadget with appropriate features. The Gadgets abstract class is not aware of the features that each gadget should have.
Sample output of above program.
C:\Python\programs\designpatterns>pep8 --first factory1.py

C:\Python\programs\designpatterns>python factory1.py
##### MOBILE FEATURE LIST #####
5.5 inch, 1280 x 720 Pixels, TFT LCD IPS
MediaTek MTK6737 1.3GHz, Quad Core, 1.3 GHz
Voice Call, Phonebook
Internal Storage 32GB, RAM 3GB


##### TABLET FEATURE LIST #####
5.5 inch, 1280 x 720 Pixels, TFT LCD IPS
Internal Storage 32GB, RAM 3GB
MediaTek MTK6737 1.3GHz, Quad Core, 1.3 GHz

C:\Python\programs\designpatterns>