Factory Method

Dated Sep 16, 2026; last modified on Wed, 16 Sep 2026

A factory is a helper that makes the right kind of object for you so you don’t have to decide. That said, factories are polarizing because some engineers view them as examples of engineering.

Simple Factory

Factory shows up when requirements say something like “support different notification types”, e.g.,

public interface INotification
{
  void Send(string message);
}

public class EmailNotification : INotification
{
  public void Send(string message) { ... }
}

public class SmsNotification : INotification
{
  public void Send(string message) { ... }
}

enum NotificationType { Email, Sms }

public static class NotificationFactory
{
  public static INotification Create(NotificationType notificationType) =>
    notificationType switch {
      NotificationType.Email => new EmailNotification(),
      NotificationType.Sms => new SmsNotification(),
      _ => throw new ArgumentException("Unknown type")
    };
}

… where clients do var notification = NotificationFactory.Create(NotificationType.Sms).

How do we provide different constructor arguments, e.g.,

public class EmailNotification(IEmailSender emailSender, string emailAddress) : INotification
{
  public void Send(string message) { ... }
}

public class SmsNotification(ITwilioClient twilioClient, string phoneNumber) : INotification
{
  public void Send(string message) { ... }
}

NotificationFactory.Create needs to be aware of the constructor arguments that might not be known until invocation.

References

  1. Design Patterns | Hello Interview Low Level Design. www.hellointerview.com . Accessed Sep 16, 2026.