The Factory Pattern is a creational design pattern that provides an interface for creating objects in a super-class, but allows subclasses to alter the type of objects that will be created. The Factory Pattern is used to create objects of different classes based on a given set of conditions.
In Python, the Factory Pattern can be implemented using a combination of classes and inheritance. In this tutorial, we will explore how to implement the Factory Pattern in Python using a simple example.
Example Scenario
Let us consider an example where we want to create a program that will calculate the area of different shapes like Circle, Square and Rectangle. We want to create a Shape class as a super-class, and then create separate classes for each shape that will inherit from the Shape class. We will then create a Factory class that will use a condition to determine the type of shape to create and return an object of the appropriate class.
Step 1: Define the Super-class
The first step is to create a super-class, which will be used to define the attributes and methods that are common to all the shapes. In our example, we will create a Shape class with a method to calculate the area.
class Shape:
def calculate_area(self):
pass
Step 2: Create Sub-classes
Next, we will create sub-classes for each shape that will inherit from the Shape class. Each sub-class will implement its own version of the calculate_area() method. In our example, we will create classes for Circle, Square and Rectangle.
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def calculate_area(self):
return 3.14 * self.radius ** 2
class Square(Shape):
def __init__(self, side):
self.side = side
def calculate_area(self):
return self.side ** 2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def calculate_area(self):
return self.width * self.height
Step 3: Create the Factory Class
The next step is to create the Factory class, which will be responsible for creating objects of the appropriate class based on a given condition. In our example, we will create a ShapeFactory class that will take a string parameter and return an object of the appropriate class.
class ShapeFactory:
def create_shape(self, shape_type):
if shape_type == "circle":
return Circle(5)
elif shape_type == "square":
return Square(5)
elif shape_type == "rectangle":
return Rectangle(5, 10)
else:
return None
Step 4: Create the Main Program
Finally, we will create a main program that will use the ShapeFactory class to create objects of different shapes and calculate their areas. In our example, we will create a program that will create objects of Circle, Square and Rectangle and calculate their areas.
factory = ShapeFactory()
circle = factory.create_shape("circle")
square = factory.create_shape("square")
rectangle = factory.create_shape("rectangle")
print("Area of Circle:", circle.calculate_area())
print("Area of Square:", square.calculate_area())
print("Area of Rectangle:", rectangle.calculate_area())Output:
Area of Circle: 78.5
Area of Square: 25
Area of Rectangle: 50
Conclusion
The Factory Pattern is a useful design pattern that can be used to create objects of different classes based on a given set of conditions. In Python, the Factory Pattern can be implemented using a combination of classes and inheritance. By implementing the Factory Pattern, we can make our code more modular, extensible and easy to maintain.
In our example, we have created a Shape class as a super-class and created separate sub-classes for each shape. We have then created a ShapeFactory class that uses a condition to determine the type of shape to create and returns an object of the appropriate class. Finally, we have created a main program that uses the ShapeFactory class to create objects of different shapes and calculate their areas.
By using the Factory Pattern, we can easily add new shapes to our program by simply creating a new sub-class and updating the ShapeFactory class to include the new shape. We can also modify the behavior of our program by updating the sub-classes or the ShapeFactory class without affecting the rest of the program.
Overall, the Factory Pattern is a powerful tool for creating flexible, extensible and maintainable code. By understanding and applying this pattern, we can create more robust and scalable software systems.