Factory Method Design Pattern

Divya Khatnar
3 min readFeb 13, 2023

Factory Method design pattern is a useful tool for creating flexible, maintainable, and testable code.

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 design pattern is often used in situations where a class cannot anticipate the type of objects it needs to create, or when a class wants its subclasses to specify the objects it creates.

In the Factory Method pattern, a factory method is defined in the superclass, which returns an object of the required type. Subclasses can then override this method to create objects of their own type. The client code uses the factory method to create objects, without having to specify the exact class of the objects that are being created.

The Factory Method design pattern can be very helpful in several ways:

  1. Abstraction: It abstracts the object creation process and separates it from the client code. This makes the code more flexible, as the client code can create objects of different types without having to know the exact implementation of the objects.
  2. Encapsulation: It allows you to encapsulate the object creation process within the factory method. This makes it easier to maintain and change the object creation process in the future, without affecting the client code.
  3. Flexibility: It provides flexibility in the object creation process. By allowing subclasses to override the factory method, you can create objects of different types, depending on the specific needs of your application.
  4. Reusability: It promotes code reusability by allowing you to create objects of different types, without having to duplicate code.
  5. Testing: It makes it easier to test your code, as you can easily swap out different implementations of objects during testing, without having to modify the client code.

Steps to implement Factory Method:

  1. Define an abstract class or interface that represents the objects to be created. This class or interface should have a common interface for the objects it creates.
  2. Create concrete classes that implement the abstract class or interface, and define the behavior and attributes of the objects.
  3. Create a factory class that has a factory method. The factory method should return an instance of the object, based on the type of object that is required. The factory method should use an if statement or a switch statement to determine which type of object to create, based on the input provided by the client code.
  4. In the client code, create an instance of the factory class, and call the factory method to create objects of different types. The client code should not have to know the exact implementation of the objects.

Example of factory based design:

class Dog:
def __init__(self, name):
self._name = name

def speak(self):
return "Woof!"

class Cat:
def __init__(self, name):
self._name = name

def speak(self):
return "Meow!"

def get_pet(pet="dog"):
pets = dict(dog=Dog("Hope"), cat=Cat("Peace"))
return pets[pet]

d = get_pet("dog")
print(d.speak())

c = get_pet("cat")
print(c.speak())

In this example, get_pet is a factory method that returns a dog or a cat object, depending on the argument passed to it. The factory method makes it easy to switch between different types of objects without having to modify the client code.

The Factory Method design pattern is best used in situations where:

  1. You need to create objects of different types: It allows you to create objects of different types, depending on the specific needs of your application. This is useful when you need to create objects that have different behavior or attributes, and you don’t want the client code to have to know the exact implementation of the objects.
  2. You need to encapsulate the object creation process: It allows you to encapsulate the object creation process within the factory method. This makes it easier to maintain and change the object creation process in the future, without affecting the client code.
  3. You want to promote code reusability: It promotes code reusability by allowing you to create objects of different types, without having to duplicate code.
  4. You need to create objects dynamically: It allows you to create objects dynamically, based on runtime information. This can be useful when you need to create objects based on user input or other dynamic factors.

In conclusion, the Factory Method design pattern is a useful tool for creating flexible, maintainable, and testable code. It allows you to abstract the object creation process and provides a way to create objects of different types, depending on the specific needs of your application.

--

--