The Factory Design Pattern is one of the most commonly asked Design Pattern interview questions.
Let’s understand it in a simple, interview-friendly way with real-world examples and C# code.
Factory Design Pattern in C#
What is Factory Design Pattern?
The Factory Design Pattern is used to create objects without exposing the object creation logic to the client.
Instead of using new everywhere, you delegate object creation to a factory class.
Simple Definition (Interview Ready)
Factory Pattern provides a way to create objects without specifying the exact class of object that will be created.
Why Do We Need Factory Pattern?
Problem Without Factory
var payment = new CreditCardPayment();
payment.Pay();
Later if you change to:
var payment = new UPIPayment();
You must change code everywhere — this violates Open/Closed Principle from SOLID.
Real World Example
Think of Vehicle Factory
You ask factory:
-
"Give me Car"
-
"Give me Bike"
Factory decides what object to create.
You don't care about creation logic.
Basic Structure
Factory Pattern has:
-
Product Interface
-
Concrete Products
-
Factory Class
-
Client
Example: Payment System
Step 1: Product Interface
public interface IPayment
{
void Pay();
}
Step 2: Concrete Classes
public class CreditCardPayment : IPayment
{
public void Pay()
{
Console.WriteLine("Paid using Credit Card");
}
}
public class UPIPayment : IPayment
{
public void Pay()
{
Console.WriteLine("Paid using UPI");
}
}
public class PayPalPayment : IPayment
{
public void Pay()
{
Console.WriteLine("Paid using PayPal");
}
}
Step 3: Factory Class
public class PaymentFactory
{
public static IPayment GetPayment(string paymentType)
{
switch (paymentType)
{
case "CreditCard":
return new CreditCardPayment();
case "UPI":
return new UPIPayment();
case "PayPal":
return new PayPalPayment();
default:
throw new ArgumentException("Invalid payment type");
}
}
}
Step 4: Client Code
class Program
{
static void Main()
{
IPayment payment = PaymentFactory.GetPayment("UPI");
payment.Pay();
}
}
Output
Paid using UPI
Advantages
1. Loose Coupling
Client doesn't know about concrete classes.
2. Easy to Extend
Add new payment method:
class CryptoPayment : IPayment
Just modify factory — no change to client.
3. Follows SOLID Principles
-
Open/Closed Principle
-
Dependency Inversion
Real-Time Use Cases
Factory Pattern is used in:
-
Logging frameworks
-
Database connections
-
Payment gateways
-
Notification systems
-
Document generators
Example: Logger Factory (Real-world)
public interface ILogger
{
void Log(string message);
}
Concrete classes:
public class FileLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine("File Log: " + message);
}
}
public class DatabaseLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine("Database Log: " + message);
}
}
Factory:
public class LoggerFactory
{
public static ILogger GetLogger(string type)
{
if (type == "File")
return new FileLogger();
return new DatabaseLogger();
}
}
Usage:
ILogger logger = LoggerFactory.GetLogger("File");
logger.Log("Hello");
Factory Pattern vs Simple Object Creation
Without Factory
new Car()
new Bike()
With Factory
VehicleFactory.Create("Car")
VehicleFactory.Create("Bike")
Cleaner and scalable.
Types of Factory Patterns (Interview Question)
There are 3 types:
1. Simple Factory (Most common)
-
One factory class
-
Uses switch/if
2. Factory Method
-
Uses inheritance
-
Factory created via subclasses
3. Abstract Factory
-
Factory of factories
-
Used for related object families
When To Use Factory Pattern
Use Factory Pattern when:
-
Object creation is complex
-
Multiple object types exist
-
You want loose coupling
-
You want scalable architecture
When NOT to Use
Don't use factory when:
-
Only one object type
-
No future changes expected
-
Simple code is enough
Interview Questions
What is Factory Pattern?
Creates objects without exposing creation logic.
What Problem Does It Solve?
Reduces tight coupling and improves scalability.
Difference Between Factory and Abstract Factory?
| Factory | Abstract Factory |
|---|---|
| Creates one product | Creates families of products |
| Simple | More complex |
Short Interview Example (Best Answer)
Factory Pattern is used to create objects without exposing creation logic.
It helps achieve loose coupling and follows SOLID principles.
A factory class decides which object to create based on input.
Real Interview Tip
If interviewer asks:
Where did you use Factory Pattern?
You can say:
-
Payment gateway selection
-
Notification service (Email/SMS)
-
Logger creation
-
Database provider selection
Summary
-
Factory Pattern creates objects
-
Hides creation logic
-
Reduces coupling
-
Easy to extend
-
Very common in real projects
