The Factory Design Pattern is one of the most commonly used Creational Design Patterns in Java. Instead of creating objects directly using the new keyword, object creation logic is delegated to a factory class.

The Factory Pattern promotes loose coupling, improves maintainability and simplifies object creation. It is used extensively in enterprise applications, Spring Framework, Hibernate and Java SDK libraries.
What is Factory Design Pattern?
Factory Design Pattern provides an interface or method for creating objects without exposing object creation logic to the client.
The client requests an object from the factory instead of creating it directly.
Why Do We Need Factory Pattern?
Imagine an application that creates different types of vehicles.
Without a Factory Pattern, the client would need to know exactly which class to instantiate.
Vehicle vehicle = new Car();
As the number of vehicle types grows, object creation becomes difficult to manage.
Problem Without Factory Pattern
if(type.equals("CAR")) {
vehicle = new Car();
}
else if(type.equals("BIKE")) {
vehicle = new Bike();
}
else if(type.equals("TRUCK")) {
vehicle = new Truck();
}
This approach violates the Open/Closed Principle and makes maintenance harder.
Factory Pattern Solution
Move object creation logic into a dedicated factory.
Client
|
v
Factory
|
+---- Car
+---- Bike
+---- Truck
The client no longer needs to know how objects are created.
Step 1: Create Interface
public interface Vehicle {
void drive();
}
All vehicle implementations will follow this contract.
Step 2: Create Implementations
Car Class
public class Car
implements Vehicle {
@Override
public void drive() {
System.out.println(
"Driving Car"
);
}
}
Bike Class
public class Bike
implements Vehicle {
@Override
public void drive() {
System.out.println(
"Driving Bike"
);
}
}
Truck Class
public class Truck
implements Vehicle {
@Override
public void drive() {
System.out.println(
"Driving Truck"
);
}
}
Step 3: Create Factory Class
public class VehicleFactory {
public static Vehicle
getVehicle(String type) {
if(type.equalsIgnoreCase("CAR")) {
return new Car();
}
if(type.equalsIgnoreCase("BIKE")) {
return new Bike();
}
if(type.equalsIgnoreCase("TRUCK")) {
return new Truck();
}
throw new IllegalArgumentException(
"Invalid Vehicle Type"
);
}
}
Step 4: Client Code
public class FactoryDemo {
public static void main(String[] args) {
Vehicle vehicle =
VehicleFactory
.getVehicle("CAR");
vehicle.drive();
}
}
Output
Driving Car
How Factory Pattern Works Internally
The client requests an object from the factory.
The factory decides which concrete implementation should be created and returned.
Client
|
v
VehicleFactory
|
+---- Car
+---- Bike
+---- Truck
This hides object creation details from the client.
Real World Example
Consider a Notification System.
Email Notification
SMS Notification
Push Notification
Instead of directly creating notification objects, a factory can return the correct implementation.
Notification notification =
NotificationFactory
.getNotification("EMAIL");
Factory Pattern in Spring Framework
Spring internally uses factory-based approaches for bean creation.
A common example is FactoryBean.
FactoryBean<Object>
Spring delegates bean creation logic through factory mechanisms extensively.
Factory Pattern Advantages
- Loose Coupling
- Better Maintainability
- Centralized Object Creation
- Easier Extension
- Improved Readability
Factory Pattern Disadvantages
- Additional Classes
- Increased Complexity for Small Projects
- May Introduce Extra Abstraction
Factory Pattern vs Singleton Pattern
| Factory Pattern | Singleton Pattern |
|---|---|
| Creates Objects | Restricts Object Creation |
| Multiple Instances Possible | Single Instance |
| Focuses on Creation Logic | Focuses on Object Uniqueness |
Common Interview Questions
What is Factory Design Pattern?
A creational design pattern that delegates object creation to a factory class.
Why use Factory Pattern?
To reduce coupling and centralize object creation logic.
Which SOLID principle does Factory Pattern support?
Open/Closed Principle and Dependency Inversion Principle.
Is Factory Pattern used in Spring Framework?
Yes. Spring uses factory-based mechanisms internally for bean creation and management.
When should Factory Pattern be used?
When object creation logic becomes complex or multiple implementations exist.
Best Practices
- Use interfaces for products.
- Keep factory logic centralized.
- Avoid overly complex factory implementations.
- Combine Factory Pattern with Dependency Injection where appropriate.
- Follow SOLID principles.
Frequently Asked Questions (FAQ)
Can Factory Pattern return different object types?
Yes. That is one of its primary purposes.
Can Factory Pattern improve maintainability?
Yes. It centralizes object creation and reduces direct dependencies.
Is Factory Pattern a Creational Pattern?
Yes. Factory Pattern belongs to the Creational Design Pattern category.
Conclusion
The Factory Design Pattern simplifies object creation and promotes loose coupling between application components. It is widely used across enterprise Java applications, Spring Framework and design pattern-based architectures. Mastering Factory Pattern is essential for interviews and writing maintainable software solutions.
Keywords
Factory Design Pattern, Factory Pattern Java, Creational Design Patterns, Java Design Patterns, Factory Method Pattern, Factory Pattern Interview Questions, Spring FactoryBean, Object Creation Pattern, Design Patterns Java, Factory Example Java, Core Java Interview Questions, Java Architecture, OOP Design Patterns, Software Design Patterns, Java Best Practices
Reviewed by 13thGhost
on
2:34 AM
Rating:
No comments: