Open In App

Abstract Factory Method – Python Design Patterns

Last Updated : 06 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Abstract Factory Method is a Creational Design pattern that allows you to produce the families of related objects without specifying their concrete classes. Using the abstract factory method, we have the easiest ways to produce a similar type of many objects. 
It provides a way to encapsulate a group of individual factories. Basically, here we try to abstract the creation of the objects depending on the logic, business, platform choice, etc.

Problem we face without Abstract Factory Method:

Imagine you want to join one of the elite batches of GeeksforGeeks. So, you will go there and ask about the Courses available, their Fee structure, their timings, and other important things. They will simply look at their system and will give you all the information you required. Looks simple? Think about the developers how they make the system so organized and how their website is so lubricative.

Developers will make unique classes for each course which will contain its properties like Fee structure, timings, and other things. But how they will call them and how will they instantiate their objects?

Here arises the problem, suppose initially there are only 3-4 courses available at GeeksforGeeks, but later they added 5 new courses. 
So, we have to manually instantiate their objects which is not a good thing according to the developer’s side. 

abstract-factory-python

Abstract Factory

Diagrammatic representation of Problems without using Abstract Factory Method

Note: Following code is written without using the abstract factory method 

Python3
# Python Code for object
# oriented concepts without
# using the Abstract factory
# method in class

class DSA:

    """ Class for Data Structure and Algorithms """

    def price(self):
        return 11000

    def __str__(self):
        return "DSA"

class STL:

    """Class for Standard Template Library"""

    def price(self):
        return 8000

    def __str__(self):
        return "STL"

class SDE:

    """Class for Software Development Engineer"""

    def price(self):
        return 15000

    def __str__(self):
        return 'SDE'

# main method
if __name__ == "__main__":

    sde = SDE()    # object for SDE class
    dsa = DSA()    # object for DSA class
    stl = STL()    # object for STL class

    print(f'Name of the course is {sde} and its price is {sde.price()}')
    print(f'Name of the course is {dsa} and its price is {dsa.price()}')
    print(f'Name of the course is {stl} and its price is {stl.price()}')

Solution by using Abstract Factory Method:

Its solution is to replace the straightforward object construction calls with calls to the special abstract factory method. Actually, there will be no difference in the object creation but they are being called within the factory method. 
Now we will create a unique class whose name is Course_At_GFG which will handle all the object instantiation automatically. Now, we don’t have to worry about how many courses we are adding after some time.

solution-abstract-factory-pattern-method

solution using abstract factory pattern

Python3
# Python Code for object
# oriented concepts using
# the abstract factory
# design pattern

import random

class Course_At_GFG:

    """ GeeksforGeeks portal for courses """

    def __init__(self, courses_factory = None):
        """course factory is out abstract factory"""

        self.course_factory = courses_factory

    def show_course(self):

        """creates and shows courses using the abstract factory"""

        course = self.course_factory()

        print(f'We have a course named {course}')
        print(f'its price is {course.Fee()}')


class DSA:

    """Class for Data Structure and Algorithms"""

    def Fee(self):
        return 11000

    def __str__(self):
        return "DSA"

class STL:

    """Class for Standard Template Library"""

    def Fee(self):
        return 8000

    def __str__(self):
        return "STL"

class SDE:

    """Class for Software Development Engineer"""

    def Fee(self):
        return 15000

    def __str__(self):
        return 'SDE'

def random_course():

    """A random class for choosing the course"""

    return random.choice([SDE, STL, DSA])()


if __name__ == "__main__":

    course = Course_At_GFG(random_course)

    for i in range(5):
        course.show_course()

Class Diagram for Abstract Factory Method:

Let’s look at the class diagram considering the example of Courses at GeeksforGeeks. 
Fee structure of all the available courses at GeeksforGeeks 
 

abstract-factory-class-diagram

class diagram for the abstract factory pattern


Timings of all the available courses at GeeksforGeeks 
 

abstract-factory-class-diagram-2

class diagram 2 abstract method pattern

Advantages of using Abstract Factory method:

This pattern is particularly useful when the client doesn’t know exactly what type to create. 

  1. It is easy to introduce new variants of the products without breaking the existing client code.
  2. Products which we are getting from the factory are surely compatible with each other.

Disadvantages of using Abstract Factory method:

  1. Our simple code may become complicated due to the existence of a lot of classes.
  2. We end up with a huge number of small files i.e, cluttering of files.

Applicability:

  1. Most commonly, abstract factory method pattern is found in the sheet metal stamping equipment used in the manufacture of automobiles.
  2. It can be used in a system that has to process reports of different categories such as reports related to input, output, and intermediate transactions.


Previous Article
Next Article

Similar Reads

Differences Between Abstract Factory and Factory Design Patterns
Exploring the differences between Abstract Factory and Factory Design Patterns is essential for creating efficient software. Despite both being about making objects, they have different roles. This article breaks down these differences to help developers choose the right approach. Important Topics for Factory vs. Abstract Factory Design Pattern Wha
4 min read
Abstract Factory Pattern | C++ Design Patterns
Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are compatible and properly configured. In this artic
6 min read
Abstract Factory Pattern | JavaScript Design Patterns
Abstract Factory Pattern is to abstract the process of object creation by defining a family of related factory methods, each responsible for creating a different type of object. These factory methods are organized within an abstract factory interface or class, and the client code uses this interface to create objects. Important Topics for the Abstr
6 min read
Implementing Web Crawler using Abstract Factory Design Pattern in Python
In the Abstract Factory design pattern, every product has an abstract product interface. This approach facilitates the creation of families of related objects that is independent of their factory classes. As a result, you can change the factory at runtime to get a different object – simplifies the replacement of the product families. In this design
5 min read
Factory Method - Python Design Patterns
Factory Method is a Creational Design Pattern that allows an interface or a class to create an object, but lets subclasses decide which class or object to instantiate. Using the Factory method, we have the best ways to create an object. Here, objects are created without exposing the logic to the client, and for creating the new type of object, the
5 min read
Factory Method | Java Design Patterns
The factory design pattern is a creational design pattern. It states the best way to create an object without telling the exact class of object that will be created. It is used when we have a super-class with multiple sub-classes and based on input, we want to return one of the sub-class. Important Topics for the Factory Method in Java Design Patte
7 min read
Factory Method Pattern | C++ Design Patterns
Factory Method Pattern provides an interface for creating objects but leaves the actual object instantiation to derived classes. This allows for flexibility in object creation and promotes loose coupling between the creator (client code) and the concrete products. Important Topics for the Factory Method Pattern in C++ Design Patterns Real-World Exa
8 min read
Abstract Factory Pattern
The Abstract Factory Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes, in simpler terms the Abstract Factory Pattern is a way of organizing how you create groups of things that are related to each other. Important Topics for the Abstract
10 min read
What to use - interface or abstract class to implement Factory pattern?
Both interfaces and abstract classes can be used to implement the Factory Pattern. The choice between them depends on the specific requirements of your application and design preferences. In general, if you need a simple factory pattern with just one method to create objects, an interface might be a better choice. If you need a more complex factory
1 min read
Difference Between Builder Design Pattern and Factory Design Pattern
Design patterns provide proven solutions to common problems in software design. The Builder and Factory patterns are two popular creational design patterns. The Builder pattern constructs complex objects step by step. In contrast, the Factory pattern creates objects without specifying their exact class. Both patterns streamline object creation but
7 min read