Simple Factory Method: The First Child

Ayush Gautam
3 min readMar 2, 2023

--

The Simple Factory Design Pattern is the most basic of all the Factory patterns. It’s a creational pattern that allows creating objects without having to specify the exact class of the object that will be created. Instead, the Simple Factory defines a single factory method that takes an input parameter and returns an object of a specific class based on that parameter.

The First Child

In this article, we’ll explore how to implement the Simple Factory Design Pattern in Python.

The Problem

Let’s consider a simple example where we have a class hierarchy that consists of different types of vehicles, such as cars, buses, and trucks. We want to create objects of these different types of vehicles without having to know the specific class of the vehicle that we want to create.

The Solution

The Simple Factory Design Pattern provides a solution to this problem. We can create a simple factory class that has a single method for creating objects of the different types of vehicles based on an input parameter.

Here’s how we can implement the Simple Factory Design Pattern in Python:

Step 1: Define the Vehicle class hierarchy.

class Vehicle:
def __init__(self, wheels):
self._wheels = wheels
def get_wheels(self):
return self._wheels
class Car(Vehicle):
def __init__(self):
super().__init__(4)
class Bus(Vehicle):
def __init__(self):
super().__init__(6)
class Truck(Vehicle):
def __init__(self):
super().__init__(8)

In this step, we define a Vehicle base class and three subclasses: Car, Bus, and Truck. Each subclass represents a different type of vehicle and has a different number of wheels.

Step 2: Create a simple factory class.

class VehicleFactory:
def create_vehicle(self, vehicle_type):
if vehicle_type == "car":
return Car()
elif vehicle_type == "bus":
return Bus()
elif vehicle_type == "truck":
return Truck()
else:
raise ValueError("Invalid vehicle type."

In this step, we create a VehicleFactory class with a single method create_vehicle(). This method takes an input parameter vehicle_type and returns an object of the corresponding vehicle type. If the input parameter is not valid, the method raises a ValueError.

Step 3: Use the factory to create objects.

factory = VehicleFactory()
car = factory.create_vehicle("car")
print(car.get_wheels()) # Output: 4
bus = factory.create_vehicle("bus")
print(bus.get_wheels()) # Output: 6
truck = factory.create_vehicle("truck")
print(truck.get_wheels()) # Output: 8

In this step, we create a VehicleFactory object and use it to create objects of different vehicle types by calling the create_vehicle() method and passing the corresponding vehicle type as an input parameter. We then print the number of wheels of each vehicle to verify that they were created successfully.

Conclusion

The Simple Factory Design Pattern is a simple and effective way to create objects of different types without having to know their exact class. In this article, we’ve shown how to implement the Simple Factory Design Pattern in Python using a class hierarchy of vehicles as an example. By using a simple factory class with a single factory method, we were able to create objects of different types of vehicles with ease.

--

--

Ayush Gautam

Ayush, a recent CS graduate with a passion for coding. Committed to writing clean, maintainable code and eager to learn and contribute to the community.