๐ง What Is the Factory Method Pattern?
The Factory Method Pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.
This pattern helps delegate the instantiation logic to child classes, making your code more flexible, testable and maintainable.
๐งฉ When to Use It
Use the Factory Method Pattern when:
The exact type of the object to create is determined at runtime
You want to avoid tight coupling between client code and concrete classes
You want to enforce a consistent object creation process across a system
Typical use cases include:
Frameworks and libraries that allow client extension
Plugin architectures
GUI toolkits for dynamic component rendering
Object pooling systems
๐ ๏ธ How It Works
The pattern defines a method in a base class that is responsible for creating objects. Subclasses override this method to create specific types.
C# Example: Creating Notification Senders
public abstract class Notification
{
public abstract void Send(string message);
}
public class EmailNotification : Notification
{
public override void Send(string message)
{
Console.WriteLine($"Email: {message}");
}
}
public class SmsNotification : Notification
{
public override void Send(string message)
{
Console.WriteLine($"SMS: {message}");
}
}
// Creator class
public abstract class NotificationCreator
{
public abstract Notification CreateNotification();
public void Notify(string message)
{
var notification = CreateNotification();
notification.Send(message);
}
}
// Concrete creators
public class EmailNotificationCreator : NotificationCreator
{
public override Notification CreateNotification() => new EmailNotification();
}
public class SmsNotificationCreator : NotificationCreator
{
public override Notification CreateNotification() => new SmsNotification();
}
Usage
NotificationCreator creator = new EmailNotificationCreator();
creator.Notify("Factory Method Pattern is awesome");
creator = new SmsNotificationCreator();
creator.Notify("Keep your code clean");
โ๏ธ Pros and Cons
โ Pros
Eliminates tight coupling between client code and concrete classes
Makes code easier to extend with new products
Promotes single responsibility and open-closed principles
Simplifies object creation logic
โ Cons
Adds complexity with additional classes and interfaces
May be overkill if object creation is simple or unlikely to change
Requires discipline to use correctly in small-scale projects
๐งช Testing Benefits
Since creation logic is abstracted, testing becomes easier:
Swap out concrete factories with mocks or stubs
Test object creation independently from consumers
Promote more modular architecture
๐ฆ Real-World Analogies
Document editors like Word or Google Docs use factory methods to instantiate the correct document type when you open a file
Online stores use factory methods to generate different types of payment processors based on the selected payment method
Game engines use it to spawn different types of enemies or game objects depending on level logic
๐งฑ Related Patterns
Abstract Factory: Creates families of related objects without specifying their concrete classes
Builder: Focuses on step-by-step construction rather than just instantiation
Prototype: Clones existing objects instead of creating new ones from scratch
๐ Final Thoughts
The Factory Method Pattern provides a powerful way to make object creation more abstract and flexible. It aligns well with the principles of SOLID design, especially the open-closed principle. Use it when you anticipate the need to vary object creation in a scalable and testable way.