what is factory pattern
Loading
what is factory pattern
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Cynthia SathuragiriRank #937 · 2.2K reputationPosted Jun 16, 2026, 8:57 AM EDT
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.
Sudarshan HajareRank #741 · 3.1K reputationPosted Jun 16, 2026, 10:11 PM EDT
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.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Jun 18, 2026, 11:25 PM EDT
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/