Factory Pattern
The Factory Design Pattern is a creational design pattern that provides an interface for creating objects without exposing the object creation logic to the client. Instead of using new directly, the client asks a factory to create the object.
Why Use the Factory Pattern?
- Encapsulates object creation logic.
- Reduces coupling between client code and concrete classes.
- Makes code easier to extend and maintain.
- Supports the Open/Closed Principle (open for extension, closed for modification).
Factory Method vs Factory Method Pattern
Generic Factory Method (just a method that creates objects)
Any method that creates and returns an object can be called a factory method.
public static Vehicle createVehicle() {
return new Car();
}Here, “factory method” simply means a method responsible for object creation.
Both Simple Factory and Abstract Factory contain such methods.
Factory Method Pattern (GoF Design Pattern)
This is a specific design pattern from the Gang of Four.
It intends to define an interface for creating an object while allowing subclasses to decide which class to instantiate.
Types of Factory Patterns
1. Simple Factory Pattern
A single factory class creates different objects based on input.
Vehicle vehicle = VehicleFactory.createVehicle("CAR");Creates one object. The factory decides which object to create using conditions.
2. Factory Method Pattern
Defines an interface for creating objects, but subclasses decide which class to instantiate.
VehicleFactory factory = new CarFactory();
Vehicle vehicle = factory.createVehicle();The chosen factory already knows which object to create.
3. Abstract Factory Pattern
Creates families of related objects.
Get Syed Nazeer’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
VehicleFactory factory = new IndiaVehicleFactory();
Car car = factory.createCar();
Bike bike = factory.createBike();Creates a family of related objects (IndiaCar and IndiaBike).
Advantages:
→ Loose coupling
→ Better maintainability
→ Centralised object creation
→ Easy to add new product types
→ Supports SOLID principles
Disadvantages:
→ More classes and complexity
→ The factory may become large if many product types exist
When to Use:
Use the Factory Pattern when:
- The exact type of object is decided at runtime.
- Object creation is complex.
- You want to hide the creation logic from clients.
- Multiple implementations share a common interface.
Examples: database drivers, logging frameworks, payment gateways, notification services, UI component libraries, and parsers.
Simple Example (Java)
Without Factory
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.drive();
}
}
class Car {
public void drive() {
System.out.println("Driving a car");
}
}The client is tightly coupled to Car.
1. With Simple Factory Pattern
Product Interface
interface Vehicle {
void drive();
}Concrete Products
class Car implements Vehicle {
public void drive() {
System.out.println("Driving a car");
}
}class Bike implements Vehicle {
public void drive() {
System.out.println("Riding a bike");
}
}Factory Class
class VehicleFactory {
public static Vehicle createVehicle(String type) {
if (type.equalsIgnoreCase("car")) {
return new Car();
}
if (type.equalsIgnoreCase("bike")) {
return new Bike();
}
throw new IllegalArgumentException("Unknown vehicle type");
}
}Client Code
public class Main {
public static void main(String[] args) {
Vehicle vehicle = VehicleFactory.createVehicle("car");
vehicle.drive();
}
}Output
Driving a carFor the Vehicle Factory Pattern example, the UML class diagram would look like this:
+----------------+
| Vehicle | <<interface>>
+----------------+
| + drive() |
+----------------+
^
|
-------------------
| |
| |
+--------+ +--------+
| Car | | Bike |
+--------+ +--------+
| | | |
+--------+ +--------+
|+drive()| |+drive()|
+--------+ +--------+
+----------------------+
| VehicleFactory |
+----------------------+
| |
+----------------------+
| +createVehicle(type) |
+----------------------+
|
| creates
v
+--------------+
| Vehicle |
+--------------+
+-------------+
| Client |
+-------------+
| |
+-------------+
|
| uses
v
+----------------------+
| VehicleFactory |
+----------------------+This diagram clearly shows the three key participants of the Factory Pattern:
- Product →
Vehicle - Concrete Products →
Car,Bike - Factory →
VehicleFactory - Client → Uses the factory instead of creating objects directly.
2. With Factory Method Pattern
Product Interface
interface Vehicle {
void drive();
}Concrete Products
class Car implements Vehicle {
public void drive() {
System.out.println("Driving a car");
}
}class Bike implements Vehicle {
public void drive() {
System.out.println("Riding a bike");
}
}Creator Factory
interface VehicleFactory {
Vehicle createVehicle();
}Concrete Factories
class CarFactory implements VehicleFactory {
@Override
public Vehicle createVehicle() {
return new Car();
}
}class BikeFactory implements VehicleFactory {
@Override
public Vehicle createVehicle() {
return new Bike();
}
}Client Code
public class Main {
public static void main(String[] args) {
VehicleFactory factory = new CarFactory();
Vehicle vehicle = factory.createVehicle();
vehicle.drive();
}
}Output
Driving a carFor the Vehicle Factory Method Pattern example, the UML class diagram would look like this:
<<interface>>
VehicleFactory
----------------
+ createVehicle() : Vehicle
^
|
----------------+----------------
| |
| |
+----------------+ +----------------+
| CarFactory | | BikeFactory |
+----------------+ +----------------+
| | | |
+----------------+ +----------------+
| +createVehicle()| | +createVehicle()|
+----------------+ +----------------+
| |
| creates | creates
v v
<<interface>>
Vehicle
----------------
+ drive() : void
^
|
--------------+--------------
| |
| |
+-------------+ +-------------+
| Car | | Bike |
+-------------+ +-------------+
| | | |
+-------------+ +-------------+
| +drive() | | +drive() |
+-------------+ +-------------+UML with Client
Client(Main)
|
| uses
v
<<interface>>
VehicleFactory
+ createVehicle(): Vehicle
^
|
----------------+----------------
| |
| |
CarFactory BikeFactory
| |
| creates | creates
v v
<<interface>>
Vehicle
+ drive()
^
|
--------------+--------------
| |
Car BikeRelationship Summary:
3. With Abstract Factory Pattern
The Abstract Factory Pattern provides an interface for creating families of related objects without specifying their concrete classes.
Vehicle Example
Suppose you manufacture vehicles for different regions:
- India Factory → Bike, Car
- USA Factory → Bike, Car
The client should not know which concrete classes are being created.
1. Product Interfaces
interface Car {
void drive();
}interface Bike {
void ride();
}2. Concrete Products
India Vehicles
class IndiaCar implements Car {
@Override
public void drive() {
System.out.println("Driving India Car");
}
}class IndiaBike implements Bike {
@Override
public void ride() {
System.out.println("Riding India Bike");
}
}USA Vehicles
class USACar implements Car {
@Override
public void drive() {
System.out.println("Driving USA Car");
}
}
class USABike implements Bike {
@Override
public void ride() {
System.out.println("Riding USA Bike");
}
}3. Abstract Factory
interface VehicleFactory {
Car createCar();
Bike createBike();
}Notice that the factory creates a family of products (Car and Bike).
4. Concrete Factories
India Factory
class IndiaVehicleFactory implements VehicleFactory {
@Override
public Car createCar() {
return new IndiaCar();
}
@Override
public Bike createBike() {
return new IndiaBike();
}
}USA Factory
class USAVehicleFactory implements VehicleFactory {
@Override
public Car createCar() {
return new USACar();
}
@Override
public Bike createBike() {
return new USABike();
}
}5. Client Code
public class Main {
public static void main(String[] args) {
VehicleFactory factory = new IndiaVehicleFactory();
Car car = factory.createCar();
Bike bike = factory.createBike();
car.drive();
bike.ride();
}
}Output
Driving India Car
Riding India BikeHere’s a UML Class Diagram for the Abstract Factory Vehicle example:
+------------------+
| VehicleFactory |
+------------------+
| +createCar() |
| +createBike() |
+------------------+
^
------------------|------------------
| |
| |
+---------------------------+ +---------------------------+
| IndiaVehicleFactory | | USAVehicleFactory |
+---------------------------+ +---------------------------+
| +createCar() | | +createCar() |
| +createBike() | | +createBike() |
+---------------------------+ +---------------------------+
| | | |
| creates | creates | creates | creates
v v v v
+---------------+ +---------------+
| IndiaCar | | USACar |
+---------------+ +---------------+
| +drive() | | +drive() |
+---------------+ +---------------+
^ ^
| |
| implements | implements
| |
+---------------+ +---------------+
| Car | | Car |
+---------------+ +---------------+
| +drive() |
+---------------+
+---------------+ +---------------+
| IndiaBike | | USABike |
+---------------+ +---------------+
| +ride() | | +ride() |
+---------------+ +---------------+
^ ^
| |
| implements | implements
| |
+---------------+ +---------------+
| Bike | | Bike |
+---------------+ +---------------+
| +ride() |
+---------------+A cleaner UML using standard notation:
<<interface>>
VehicleFactory
+----------------------+
+ createCar(): Car
+ createBike(): Bike
+----------------------+
^
|
+--------------+--------------+
| |
+-------------------+ +-------------------+
|IndiaVehicleFactory| | USAVehicleFactory |
+-------------------+ +-------------------+
|+createCar() | |+createCar() |
|+createBike() | |+createBike() |
+-------------------+ +-------------------+
| creates | creates
v v
<<interface>> <<interface>>
Car Bike
+---------+ +---------+
+ drive() | + ride() |
+---------+ +---------+
^ ^
| |
+-----------+ +-----------+
| IndiaCar | | IndiaBike |
+-----------+ +-----------+
^ ^
| |
+-----------+ +-----------+
| USACar | | USABike |
+-----------+ +-----------+Relationship Summary
- VehicleFactory → Abstract Factory
- IndiaVehicleFactory, USAVehicleFactory → Concrete Factories
- Car, Bike → Abstract Products
- IndiaCar, USACar, IndiaBike, USABike → Concrete Products
- Client depends only on:
VehicleFactoryCarBike
Why is it called “Abstract Factory”?
Because:
VehicleFactoryis an abstract factory (an interface).- It defines methods for creating a group of related products.
- Concrete factories (
IndiaVehicleFactory,USAVehicleFactory) decide which actual objects to create.
Factory Method vs Abstract Factory
Think of it this way:
- Factory Method → “Give me a Vehicle.”
- Abstract Factory → “Give me all vehicles for a specific market (Car + Bike).”
Real-World Example
Imagine a notification system:
Notification notification =
NotificationFactory.create("EMAIL");The factory decides whether to create:
EmailNotificationSMSNotificationPushNotification
The client only knows about the Notification interface.







