Can anyone explain Factory Method Pattern
Loading
Can anyone explain Factory Method 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
Jaish MathewsRank #225 · 11K reputationPosted Dec 16, 2024, 7:15 AM EST
The Factory Method Pattern is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It helps to encapsulate the object creation process and promotes code flexibility and reusability.
Key Characteristics
C# Example: Factory Method Pattern
Scenario: Notification Sender
Let's say we have a notification system that supports different notification types: Email and SMS.
Step 1: Define a Product Interface
public interface INotification { void Notify(string message); }Step 2: Create Concrete Implementations
public class EmailNotification : INotification { public void Notify(string message) { Console.WriteLine($"Email Notification: {message}"); } } public class SmsNotification : INotification { public void Notify(string message) { Console.WriteLine($"SMS Notification: {message}"); } }Step 3: Define the Creator (Abstract Class)
The creator declares the factory method, which returns an object of type
INotification.public abstract class NotificationFactory { public abstract INotification CreateNotification(); }Step 4: Create Concrete Factories
Concrete factories override the factory method to specify the type of object to be created.
public class EmailNotificationFactory : NotificationFactory { public override INotification CreateNotification() { return new EmailNotification(); } } public class SmsNotificationFactory : NotificationFactory { public override INotification CreateNotification() { return new SmsNotification(); } }Step 5: Client Code
The client interacts with the factory instead of directly instantiating the objects.
class Program { static void Main(string[] args) { NotificationFactory factory; // Email Notification factory = new EmailNotificationFactory(); var emailNotifier = factory.CreateNotification(); emailNotifier.Notify("This is an email notification."); // SMS Notification factory = new SmsNotificationFactory(); var smsNotifier = factory.CreateNotification(); smsNotifier.Notify("This is an SMS notification."); } }How It Works
Mainmethod) works with an abstract factory (NotificationFactory) and does not depend on the specific classes (EmailNotificationorSmsNotification).EmailNotificationFactoryandSmsNotificationFactory) encapsulate the object creation process.Output
Advantages
Disadvantages
This pattern is widely used in scenarios like logging frameworks, database connections, and notification systems where the type of object created needs to be dynamic.
Sangeetha SRank #212 · 11.4K reputationPosted Dec 16, 2024, 12:22 AM EST
The Factory Method Pattern is a creational design pattern used in object-oriented programming. Its main purpose is to define an interface for creating an object, but allow subclasses to alter the type of objects that will be created. Here's a quick breakdown:
Benefits:
Rajeesh MenothRank #118 · 18.4K reputationPosted Dec 16, 2024, 12:37 AM EST
Hi,
The class to instantiate objects without specifying the exact class of the object that will be created. Overall this promotes loose coupling to the Open/Closed Principle (open for extension, closed for modification).
Sangeetha has already covered the details of the components, so I won't repeat that information here.
You can go through some of the example articles -
https://www.geeksforgeeks.org/factory-method-for-designing-pattern/
https://www.c-sharpcorner.com/blogs/factory-design-pattern-in-c-sharp