Open links in new tab
  1. The Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. This pattern is particularly useful when the exact type of object to be created isn't known until runtime.

    Example Implementation

    Let's consider an example where we need to create different types of shapes (Circle, Square, Rectangle). Instead of creating these shapes directly, we'll use a factory method to create them.

    Step 1: Define the Shape Interface

    First, we define an abstract class Shape that will be the base class for all shapes.

    import abc

    class Shape(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def calculate_area(self):
    pass

    @abc.abstractmethod
    def calculate_perimeter(self):
    pass
    Copied!

    Step 2: Create Concrete Shape Classes

    Next, we create concrete classes for different shapes that inherit from the Shape class.

    class Rectangle(Shape):
    def __init__(self, height, width):
    self.height = height
    self.width = width

    def calculate_area(self):
    return self.height * self.width

    def calculate_perimeter(self):
    return 2 * (self.height + self.width)

    class Square(Shape):
    def __init__(self, width):
    self.width = width

    def calculate_area(self):
    return self.width ** 2

    def calculate_perimeter(self):
    return 4 * self.width

    class Circle(Shape):
    def __init__(self, radius):
    self.radius = radius

    def calculate_area(self):
    return 3.14 * self.radius * self.radius

    def calculate_perimeter(self):
    return 2 * 3.14 * self.radius
    Copied!
  1. Factory Method - Python Design Patterns - GeeksforGeeks

    Jul 12, 2025 · 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 …

  2. How to Use the Factory Pattern in Python - A Practical …

    Feb 9, 2026 · In this tutorial, you'll learn what the factory pattern is, why it's useful, and how to implement it in Python. We'll build practical examples that show you …

  3. Factory Method in Python / Design Patterns

    Learn how to use the Factory Method design pattern to create objects without specifying their concrete classes. See a conceptual example, a complex example, …

  4. Factory Design Pattern in Python: Concepts, Usage, and Best Practices

    Mar 23, 2025 · This blog post will delve into the fundamental concepts, usage methods, common practices, and best practices of the Factory Design Pattern in Python.

  5. Factory: Encapsulating Object Creation — Python 3 …

    Learn how to use the factory pattern to create objects of different types in Python. See examples of simple and polymorphic factories, and how to prevent direct …

  6. Design Patterns in Python: Factory Method - Medium

    Sep 17, 2023 · The Factory Method design pattern offers a systematic way to create objects while keeping code maintainable and adaptable. It excels in …

  7. The Factory Method Design Pattern in Python - Stack Abuse

    Sep 11, 2023 · Learn how to use the Factory Method pattern to create objects of different types with a single interface. See an example of a shape library that allows users to choose and create shapes …

  8. Factory Design Patterns in Python - Dagster

    Factory patterns help create reusable components in data engineering. Learn how to apply them in Python using Dagster.

  9. Python Design Patterns - Factory Pattern - Online Tutorials Library

    Factory patterns are implemented in Python using factory method. When a user calls a method such that we pass in a string and the return value as a new object is implemented through factory method.

  10. People also ask
    Loading
    Unable to load answer