Some text some message..
Back Factory System Design Pattern: Factory Pattern Or creational design pattern 07 Aug, 2025

The Factory System Design Pattern, also known simply as the Factory Pattern, is a creational design pattern used in software development to manage the creation of objects. It provides an interface or method for creating objects in a superclass, but allows subclasses or implementing classes to alter the type of objects that will be created.


🔧 Definition

Factory Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses.


💡 Why Use Factory Pattern?

  • To avoid tight coupling between object creation and usage.

  • To adhere to the Open/Closed Principle (open for extension, closed for modification).

  • To manage and encapsulate object creation logic in one place.

  • To create objects at runtime based on input or configuration.


🏗️ Types of Factory Patterns

  1. Simple Factory (Not a true GoF pattern, but commonly used)

  2. Factory Method Pattern (GoF pattern)

  3. Abstract Factory Pattern (Advanced)


✅ Example: Simple Factory in Python

class Car:
    def drive(self):
        return "Driving a car"

class Bike:
    def drive(self):
        return "Riding a bike"

class VehicleFactory:
    @staticmethod
    def get_vehicle(vehicle_type):
        if vehicle_type == "car":
            return Car()
        elif vehicle_type == "bike":
            return Bike()
        else:
            raise ValueError("Unknown vehicle type")

# Client code
vehicle = VehicleFactory.get_vehicle("car")
print(vehicle.drive())  # Output: Driving a car

🏭 Factory Method Pattern (More Structured)

from abc import ABC, abstractmethod

# Product interface
class Vehicle(ABC):
    @abstractmethod
    def drive(self):
        pass

# Concrete products
class Car(Vehicle):
    def drive(self):
        return "Driving a car"

class Bike(Vehicle):
    def drive(self):
        return "Riding a bike"

# Creator (Factory Interface)
class VehicleFactory(ABC):
    @abstractmethod
    def create_vehicle(self) -> Vehicle:
        pass

# Concrete Factories
class CarFactory(VehicleFactory):
    def create_vehicle(self):
        return Car()

class BikeFactory(VehicleFactory):
    def create_vehicle(self):
        return Bike()

# Client code
def get_transport(factory: VehicleFactory):
    vehicle = factory.create_vehicle()
    print(vehicle.drive())

get_transport(CarFactory())   # Driving a car
get_transport(BikeFactory())  # Riding a bike

🧠 When to Use Factory Pattern?

✅ When the object creation is complex or repetitive
✅ When you want to decouple object creation from its usage
✅ When your code needs to work with various types of related objects


📌 Key Benefits

  • Promotes loose coupling

  • Simplifies code maintenance

  • Makes code more scalable and extendable

  • Centralizes object creation logic


⚠️ Disadvantages

  • Can lead to many small classes or factory subclasses

  • Slightly increased complexity in design