loader

Ever wonder how to create objects in your code without getting bogged down by all the details? That’s where the Factory Method Pattern steps in. It lets you “order” an object without knowing exactly which class is behind the counter.

Why Bother with It?

  • Keep It Clean: You don’t need to sprinkle new everywhere. Instead, you call a method that handles creation for you.
  • Stay Flexible: Your code talks to a common interface. It doesn’t care if it’s a CheesePizza or a PepperoniPizza—it just knows it’s getting a pizza.
  • Easy to Expand: When you need a new type of object, add it by writing a new class and a new factory. No need to rewrite all your code.

How It Works (In Plain English)

  1. Set Up a Blueprint: Create an abstract class or interface that says, “Here’s what any object of this type should do.”
  2. Make Your Objects: Build concrete classes (like CheesePizza or PepperoniPizza) that follow the blueprint.
  3. The Factory Method: Write a method in a base class that’s in charge of creating these objects. Then, let subclasses decide which object gets made.
  4. Client Code Does Its Thing: Your main code just calls the factory method and gets back a ready-to-use object—no need to know all the behind-the-scenes details.

C# Code Example

C#
using System;

// Our blueprint for a pizza
public abstract class Pizza
{
    public abstract string GetName();
}

// Concrete pizza types
public class CheesePizza : Pizza
{
    public override string GetName() => "Cheese Pizza";
}

public class PepperoniPizza : Pizza
{
    public override string GetName() => "Pepperoni Pizza";
}

// The base factory class with the factory method
public abstract class PizzaFactory
{
    public abstract Pizza CreatePizza();
}

// Factories for creating specific pizzas
public class CheesePizzaFactory : PizzaFactory
{
    public override Pizza CreatePizza() => new CheesePizza();
}

public class PepperoniPizzaFactory : PizzaFactory
{
    public override Pizza CreatePizza() => new PepperoniPizza();
}

// Client code using the factories
public class Program
{
    public static void Main()
    {
        // Order a Cheese Pizza
        PizzaFactory factory = new CheesePizzaFactory();
        Pizza myPizza = factory.CreatePizza();
        Console.WriteLine(myPizza.GetName());  // Outputs: Cheese Pizza

        // Now switch to Pepperoni Pizza with no fuss
        factory = new PepperoniPizzaFactory();
        myPizza = factory.CreatePizza();
        Console.WriteLine(myPizza.GetName());  // Outputs: Pepperoni Pizza
    }
}

The Takeaway

The Factory Method Pattern is all about keeping your code neat and easy to change. Instead of having the details of object creation scattered all over, you centralize them. This makes it super simple to add new types later on or change the way things are built—all without messing with the rest of your code.

In short, if you want your code to be as smooth as ordering your favorite pizza, the Factory Method is your go-to tool. Happy coding!