Factory Method

A variant with a single parameterized creator instead of subclasses.

#When to use it

When the variants are closed and known, it doesn't make sense to create a subclass for each one.

#Example in C#

public enum Channel { Email, Sms, Push }

public class NotificationFactory
{
    public INotification Create(Channel channel) => channel switch
    {
        Channel.Email => new EmailNotification(),
        Channel.Sms   => new SmsNotification(),
        Channel.Push  => new PushNotification(),
        _ => throw new ArgumentOutOfRangeException(nameof(channel)),
    };
}
When NOT to apply

When new variants appear frequently: each case is a modification that breaks OCP.

#creational #factory