Factory Pattern
noun
say it: “FAK-tuh-ree PAT-ern” /ˈfæktəri ˈpætərn/
A design pattern that puts object creation in one place, so callers don't choose the class themselves.
What is Factory Pattern?
The Factory pattern puts object creation in one place. Callers ask the factory for "a notification for SMS" and get the right object back without knowing which class it is.
public static class NotificationFactory
{
public static INotification Create(string channel) => channel switch
{
"email" => new EmailNotification(),
"sms" => new SmsNotification(),
_ => throw new ArgumentException( quot;Unknown channel {channel}")
};
}
In a sentence
- “A factory decides whether a notification becomes an email or a text message.”
Level: Freelancer · More OOP terms at this level
Updated