Tutorials Design Patterns in C#

Factory Method Pattern — Complete Guide

Factory Method Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.

On this page

Design Patterns in C# · Lesson 2 of 69

Factory Method Pattern

GoF CoreEnterpriseCloud & Craft

GoF Core · 1 — Create & structure · ~6 min · Module 1: Creational Design Patterns

What is this?

Factory Method lets a creator decide which concrete product to instantiate through an overridable method — callers depend on an abstraction.

Why should you care?

ShopNest notifications switch Email vs Sms without if-else sprinkled in every controller.

See it live — copy this example

Paste into a C# console or class library project and run dotnet run.

public interface INotifier { void Send(string to, string body); }
public sealed class EmailNotifier : INotifier
{
    public void Send(string to, string body) => Console.WriteLine($"EMAIL {to}: {body}");
}
public sealed class SmsNotifier : INotifier
{
    public void Send(string to, string body) => Console.WriteLine($"SMS {to}: {body}");
}
public static class NotifierFactory
{
    public static INotifier Create(string channel) => channel switch
    {
        "sms" => new SmsNotifier(),
        _ => new EmailNotifier()
    };
}

NotifierFactory.Create("sms").Send("99999", "Order shipped");

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • Create returns INotifier.
  • Callers never new the concrete types.
  • A true GoF Factory Method often lives on a Creator subclass; this compact form shows the same idea.

Practice next

  1. Run the sample and try channel email vs sms.
  2. Add PushNotifier for channel push.
  3. Inject a Func via DI later.
  4. Throw on unknown channel instead of defaulting.
  5. Pass options into Create (from-address).

Remember

Create via abstraction. Centralize construction choices. Call sites stay clean.

ShopNest notify channel factory

Order events pick Email/Sms from customer preference.

Outcome: New channels plug in without editing OrderService.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Mid PDF Detailed
Dynamic Factory Selection:?
Short answer: In a more advanced system, you could dynamically choose which factory to use based on external configurations, like settings or environment variables. This would enable the system to switch between differen…
Mid PDF Detailed
Abstract Factory Interface:?
Short answer: The IUIFactory interface defines methods for creating abstract product types like buttons and checkboxes. This ensures that every concrete factory will implement the same methods, but each will provide plat…
Mid PDF Detailed
Add Methods for Removing Components:?
Short answer: The Directory class currently only has an Add() method for adding components. It could be improved by adding a Remove() method to allow for the dynamic removal of files or subdirectories. Real-world example…
Mid PDF Detailed
Redo Functionality:?
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation. Real-world exampl…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details