what is factory pattern
Loading
what is factory pattern
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajanikant HawaldarPosted Jun 19, 2026, 3:25 AM
https://www.c-sharpcorner.com/blogs/factory-design-pattern-in-c-sharp
https://www.c-sharpcorner.com/article/factory-design-pattern-in-software-development/
https://www.c-sharpcorner.com/UploadFile/8a67c0/easy-and-tricky-to-understand-the-factory-design-pattern-wit/
Sudarshan HajarePosted Jun 17, 2026, 2:11 AM
The Factory Pattern is a design pattern used in programming to create objects without exposing the exact creation logic to the code that uses them.
Simple example: Restaurant
Imagine you’re at a restaurant.
You order a “burger”.
You don’t go into the kitchen and cook it yourself.
The kitchen (the factory) decides how to prepare the burger and gives you the finished product.
In the same way, a Factory Pattern creates objects for you instead of having your code create them directly.
Without Factory Pattern
Car car = new Sedan();
Your code must know exactly which class (Sedan) to create.
With Factory Pattern
Car car = CarFactory.createCar("sedan");
Now your code only asks for a car. The factory decides which object to create.
class CarFactory {
public static Car createCar(String type) {
if (type.equals("sedan")) {
return new Sedan();
} else if (type.equals("suv")) {
return new SUV();
}
return null;
}
}
Why use it?
Keeps object creation in one place Makes code easier to maintain
Makes it easier to add new types later
Reduces coupling between classes
Real-world example
Suppose you’re building a notification system:
Notification notification =
NotificationFactory.create("email");
The factory can return:
EmailNotification
SMSNotification
PushNotification
The rest of your code doesn’t need to know which specific class is being created.
One-line definition
Factory Pattern is a way to create objects through a separate “factory” class instead of using new directly, making code more flexible and easier to manage.
Cynthia SathuragiriPosted Jun 16, 2026, 12:57 PM
Factory Pattern is a way to create objects through a factory class instead of creating them directly, making the code more flexible and easier to maintain.
In programming, instead of creating objects directly, we ask a "factory" to create them for us. The factory decides which object to create and returns it.
Example:
If an app needs different types of notifications (Email, SMS, Push), instead of writing:
Create EmailNotification
Create SMSNotification
Create PushNotification
We simply ask the NotificationFactory for a notification type. The factory creates the correct object and returns it.
This makes the code cleaner, easier to maintain, and easier to extend when new notification types are added.