The Factory Pattern is a fundamental design pattern in Object-Oriented Design that provides a way to create objects without specifying the exact class of the object that will be created. This pattern is particularly useful in scenarios where the creation process is complex or when the system needs to be decoupled from the specific classes it uses.
The Factory Pattern defines an interface for creating an object, but it is the responsibility of the subclasses to implement the method that creates the object. This allows for greater flexibility and scalability in your code, as new types of objects can be introduced without altering the existing codebase.
There are several variations of the Factory Pattern, including:
Let’s consider a simple example of a Shape
factory that creates different shapes based on the input type.
class Shape:
def draw(self):
pass
class Circle(Shape):
def draw(self):
return "Drawing a Circle"
class Square(Shape):
def draw(self):
return "Drawing a Square"
class ShapeFactory:
@staticmethod
def get_shape(shape_type):
if shape_type == 'CIRCLE':
return Circle()
elif shape_type == 'SQUARE':
return Square()
else:
return None
# Client code
shape_factory = ShapeFactory()
shape1 = shape_factory.get_shape('CIRCLE')
print(shape1.draw()) # Output: Drawing a Circle
shape2 = shape_factory.get_shape('SQUARE')
print(shape2.draw()) # Output: Drawing a Square
In this example, the ShapeFactory
class is responsible for creating Circle
and Square
objects. The client code does not need to know the details of how these shapes are created, which adheres to the principles of decoupling and encapsulation.
Now, let’s look at a more complex example using the Factory Method pattern.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def draw(self):
pass
class Circle(Shape):
def draw(self):
return "Drawing a Circle"
class Square(Shape):
def draw(self):
return "Drawing a Square"
class ShapeFactory(ABC):
@abstractmethod
def create_shape(self):
pass
class CircleFactory(ShapeFactory):
def create_shape(self):
return Circle()
class SquareFactory(ShapeFactory):
def create_shape(self):
return Square()
# Client code
circle_factory = CircleFactory()
shape1 = circle_factory.create_shape()
print(shape1.draw()) # Output: Drawing a Circle
square_factory = SquareFactory()
shape2 = square_factory.create_shape()
print(shape2.draw()) # Output: Drawing a Square
In this example, we have an abstract ShapeFactory
class with concrete factories for Circle
and Square
. Each factory is responsible for creating its respective shape, allowing for easy extension if new shapes are added in the future.
The Factory Pattern is a powerful tool in Object-Oriented Design that promotes loose coupling and enhances code maintainability. By understanding and implementing this pattern, software engineers and data scientists can improve their design skills and prepare effectively for technical interviews. Familiarity with design patterns like the Factory Pattern is essential for demonstrating problem-solving abilities and design thinking in coding interviews.