Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Builder Method - Python Design Patterns
Next article icon

Abstract Factory Method – Python Design Patterns

Last Updated : 06 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
Builder Method - Python Design Patterns
author
chaudhary_19
Improve
Article Tags :
  • Design Pattern
  • System Design
  • python-design-pattern

Similar Reads

  • Python Design Patterns Tutorial
    Design patterns in Python are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best pra
    8 min read
  • Creational Software Design Patterns in Python

    • 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 cli
      4 min read
    • Abstract Factory Method - Python Design Patterns
      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
      4 min read
    • Builder Method - Python Design Patterns
      Builder Method is a Creation Design Pattern which aims to "Separate the construction of a complex object from its representation so that the same construction process can create different representations." It allows you to construct complex objects step by step. Here using the same construction code
      5 min read
    • Prototype Method Design Pattern in Python
      The Prototype Method Design Pattern in Python enables the creation of new objects by cloning existing ones, promoting efficient object creation and reducing overhead. This pattern is particularly useful when the cost of creating a new object is high and when an object's initial state or configuratio
      6 min read
    • Singleton Method - Python Design Patterns
      Prerequisite: Singleton Design pattern | Introduction What is Singleton Method in PythonSingleton Method is a type of Creational Design pattern and is one of the simplest design patterns available to us. It is a way to provide one and only one object of a particular type. It involves only one class
      5 min read
    • Structural Software Design Patterns in Python

      • Adapter Method - Python Design Patterns
        Adapter method is a Structural Design Pattern which helps us in making the incompatible objects adaptable to each other. The Adapter method is one of the easiest methods to understand because we have a lot of real-life examples that show the analogy with it. The main purpose of this method is to cre
        4 min read
      • Bridge Method - Python Design Patterns
        The bridge method is a Structural Design Pattern that allows us to separate the Implementation Specific Abstractions and Implementation Independent Abstractions from each other and can be developed considering as single entities.The bridge Method is always considered as one of the best methods to or
        5 min read
      geeksforgeeks-footer-logo
      Corporate & Communications Address:
      A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
      Registered Address:
      K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
      GFG App on Play Store GFG App on App Store
      Advertise with us
      • Company
      • About Us
      • Legal
      • Privacy Policy
      • In Media
      • Contact Us
      • Advertise with us
      • GFG Corporate Solution
      • Placement Training Program
      • Languages
      • Python
      • Java
      • C++
      • PHP
      • GoLang
      • SQL
      • R Language
      • Android Tutorial
      • Tutorials Archive
      • DSA
      • Data Structures
      • Algorithms
      • DSA for Beginners
      • Basic DSA Problems
      • DSA Roadmap
      • Top 100 DSA Interview Problems
      • DSA Roadmap by Sandeep Jain
      • All Cheat Sheets
      • Data Science & ML
      • Data Science With Python
      • Data Science For Beginner
      • Machine Learning
      • ML Maths
      • Data Visualisation
      • Pandas
      • NumPy
      • NLP
      • Deep Learning
      • Web Technologies
      • HTML
      • CSS
      • JavaScript
      • TypeScript
      • ReactJS
      • NextJS
      • Bootstrap
      • Web Design
      • Python Tutorial
      • Python Programming Examples
      • Python Projects
      • Python Tkinter
      • Python Web Scraping
      • OpenCV Tutorial
      • Python Interview Question
      • Django
      • Computer Science
      • Operating Systems
      • Computer Network
      • Database Management System
      • Software Engineering
      • Digital Logic Design
      • Engineering Maths
      • Software Development
      • Software Testing
      • DevOps
      • Git
      • Linux
      • AWS
      • Docker
      • Kubernetes
      • Azure
      • GCP
      • DevOps Roadmap
      • System Design
      • High Level Design
      • Low Level Design
      • UML Diagrams
      • Interview Guide
      • Design Patterns
      • OOAD
      • System Design Bootcamp
      • Interview Questions
      • Inteview Preparation
      • Competitive Programming
      • Top DS or Algo for CP
      • Company-Wise Recruitment Process
      • Company-Wise Preparation
      • Aptitude Preparation
      • Puzzles
      • School Subjects
      • Mathematics
      • Physics
      • Chemistry
      • Biology
      • Social Science
      • English Grammar
      • Commerce
      • World GK
      • GeeksforGeeks Videos
      • DSA
      • Python
      • Java
      • C++
      • Web Development
      • Data Science
      • CS Subjects
      @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
      We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
      Lightbox
      Improvement
      Suggest Changes
      Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
      geeksforgeeks-suggest-icon
      Create Improvement
      Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
      geeksforgeeks-improvement-icon
      Suggest Changes
      min 4 words, max Words Limit:1000

      Thank You!

      Your suggestions are valuable to us.

      What kind of Experience do you want to share?

      Interview Experiences
      Admission Experiences
      Career Journeys
      Work Experiences
      Campus Experiences
      Competitive Exam Experiences