Abstract Factory Method

Abstract Factory Method

Abstract Factory Method

Abstract factory method is an interface for creating all specific products but leaves the actual product creation to concrete factory classes. It is a type of creational design pattern and provides various object creation mechanisms, which increase flexibility and reuse of existing code.  For example, there are three tiers of InfosecAddicts Mentorship program you can choose at random. Using the abstract factory method, we don’t have to worry about adding any other tiers of a program after some time.

The code implementation of the abstract factory method is in python and it goes thus:

import random
class InfosecAddictMentorshipPrograms:
    """InfosecAddict Mentorship Program"""
    def __init__(self, mentorship_programs=None):
        """ MentorshipPrograms """
        self.mentorship_programs = mentorship_programs
    def show_programs(self):
        """creates  and shows courses using the abstract factory"""
        program = self.mentorship_programs()
        print("Need a guide on {} mentorship?".format(program))
        print("It's duration is  {} Program".format(program.time()))
        print("it's price is {} only".format(program.fee()))
        print('\n')

class Basic:
    """ Class for data structure and algorithms"""
    def time(self):
        return "One Week Mentorship"
    def fee(self):
        return "$200"
    def __str__(self):
        return "Basic"

class Standard:
    """ Class for data structure and algorithms"""
    def time(self):
        return "One Month Mentorship"
    def fee(self):
        return "$600"
    def __str__(self):
        return "Standard"

class Premium:
    """ Class for data structure and algorithms"""
    def time(self):
        return "Two Month Mentorship"
    def fee(self):
        return "$1000"
    def __str__(self):
        return "Premium"

def shuffle_program():
    """ A random class for choosing the a mentorship proram"""
    return random.choice([Basic, Standard, Premium])()

if __name__ == '__main__':
    program = InfosecAddictMentorshipPrograms(shuffle_program)
    for i in range(5):
        program.show_programs()
The code above produced the following output:
ProBook:~/InfosecAddicts/python design pattern$ python abstract_factory.py
Need a guide on Standard mentorship?
It's duration is One Month Mentorship Program
it's price is $600 only


Need a guide on Basic mentorship?
It's duration is One Week Mentorship Program
it's price is $200 only


Need a guide on Standard mentorship?
It's duration is One Month Mentorship Program
it's price is $600 only


Need a guide on Standard mentorship?
It's duration is One Month Mentorship Program
it's price is $600 only


Need a guide on Standard mentorship?
It's duration is One Month Mentorship Program
it's price is $600 only
The result of the program actually shows how the abstract factory method was used to create objects for each program criteria like the program name, program time and it’s fees. it calls for each method that executes the output as expected.

Pros and Cons

Pros
  • Easy to add new features without exactly changing the existing code.
  • The code from the factory is surely compatible with each other.
Cons
  • It could get more complicated if there too many classes.
  • There could be a huge number of file clustering.