From Simple Factory to Factory Method: Evolution of a Design Pattern

In our previous discussion of the Simple Factory pattern, we created a single factory that produced different types of products. Today, we'll explore how this evolves into the Factory Method pattern.

Imagine our business has grown, and instead of having one factory that produces multiple types of vehicles, we now want specialized factories, each dedicated to producing a specific type of vehicle. This separation of concerns is the core idea behind the Factory Method pattern.

First, let's define our abstract product interface. In this case, we'll use vehicles:

public interface Vehicle {
    public void drive();
}

Now, let's create some concrete vehicle implementations:

public class Car implements Vehicle {
    public void drive() {
        System.out.println("Driving a car");
    }
}

public class Motorcycle implements Vehicle {
    public void drive() {
        System.out.println("Riding a motorcycle");
    }
}

public class Truck implements Vehicle {
    public void drive() {
        System.out.println("Driving a truck");
    }
}

Next, we'll define our abstract factory interface:

public interface VehicleFactory {
    public Vehicle createVehicle();
}

Now, we'll create concrete factories that implement this enterface, each responsible for creating a specific type of vehicle:

public class CarFactory implements VehicleFactory {
    public Vehicle createVehicle() {
        return new Car();
    }
}

public class MotorcycleFactory implements VehicleFactory {
    public Vehicle createVehicle() {
        return new Motorcycle();
    }
}

public class TruckFactory implements VehicleFactory {
    public Vehicle createVehicle() {
        return new Truck();
    }
}

Here's how we would use these factories in our application:

public class Application {
    public static void main(String[] args) {
        VehicleFactory carFactory = new CarFactory();
        Vehicle car = carFactory.createVehicle();
        car.drive();

        VehicleFactory motorcycleFactory = new MotorcycleFactory();
        Vehicle motorcycle = motorcycleFactory.createVehicle();
        motorcycle.drive();
    }
}

When executed, this code produces:

Driving a car
Riding a motorcycle

The Factory Method pattern consists of several key components:

  • Abstract Creator: Declares the factory method that returns a product object.
  • Concrete Creator: Implements the factory method to produce concrete products.
  • Abstract Product: Defines the interface for the type of objects the factory method creates.
  • Concrete Product: Implements the abstract product interface.

So how does the Factory Method pattern differ from the Simple Factory pattern? The primary distinction is that in the Factory Method pattern, each factory specializes in creating one type of product. This separation offers several advantages:

  • Better adherence to the Single Responsibility Principle
  • Easier to extend with new products without modifying existing code
  • More suitable when product creation is complex

However, if your application only has a few products with simple creation logic, the Simple Factory patttern might be sufficient. As with all design patterns, it's important to evaluate your specific needs rather than applying pattrens unnecessarily. Over-engineering can introduce unnecessary complexity to your codebase.

Tags: Design Patterns Factory Method java Software Architecture Object-Oriented Programming

Posted on Thu, 25 Jun 2026 17:11:38 +0000 by yurko