paint-brush
Understanding the Factory Pattern in C# — With Examplesby@devleader
28,411 reads
28,411 reads

Understanding the Factory Pattern in C# — With Examples

by Dev LeaderJanuary 19th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The Factory pattern allows developers to create objects without specifying the exact class of object that will be created.
featured image - Understanding the Factory Pattern in C# — With Examples
Dev Leader HackerNoon profile picture

As software engineers, building robust and scalable software solutions is critical to the success of any project. One way to accomplish this is through the use of design patterns, such as the Factory pattern in C#. In this article, I’ll be going over examples of the factory pattern in C# to help demonstrate what it is and how it works.


The Factory pattern allows developers to create objects without specifying the exact class of object that will be created. This pattern promotes loose coupling between objects and can enhance the maintainability and scalability of software solutions. By understanding how to use the Factory pattern effectively, software engineers can build stronger and more robust systems.


Throughout the article, I’ll cover examples of the Factory pattern, how to use it effectively, and common mistakes to avoid. Let’s dive in!


Understanding the Factory Pattern in C#

To fully appreciate the Factory pattern, it’s important to understand what design patterns are in the first place. A design pattern is an architectural blueprint for software development that can be used to solve recurring problems across different fields of software engineering. I like to think of them as useful reusable mini language-agnostic frameworks you can apply to different scenarios!


The Factory pattern is one of the most fundamental patterns in software engineering. Simply put, the Factory pattern offers a way to instantiate objects in a robust and reliable manner, especially in scenarios where creating an object without knowing the needed class is required. The Factory pattern can come in different types, including Simple Factory, Factory Method, and Abstract Factory.


To obtain a clear understanding of how the Factory pattern works, here are some brief examples of how you could use a Factory design pattern:

  • Creating shapes in a drawing application based on the name of the shape and some dimensions
  • Creating game objects within a video game
  • Creating user/role objects within an application with different permission levels

Why You Should Use the Factory Pattern in C#

The Factory pattern’s goal is to create objects in a way that is independent of both the clients and the objects being produced. Through system-level and application-level factory patterns, the Factory pattern can be implemented in C# to improve software development. There are several reasons why using the Factory pattern in C# can enhance software development.


One reason is that it allows for a clear separation of concerns, where components at different levels of the system can be specifically utilized to provide object creation services. The Factory Pattern encapsulates the creation logic of objects within a dedicated class or method. This separation means that the rest of the system does not need to know about the complexities or specifics of object creation.


Additionally, with the Factory pattern approach, there is no need to engage in complex and redundant code. This will save you both time and effort and also helps to keep the codebase maintainable. This is especially useful when you need to create the same or similar objects repeatedly.


Building a Basic Factory Pattern in C#

To gain a grasp of the Factory pattern’s implementation in C#, let’s examine an example of building a basic Factory pattern. The basic Factory Pattern is a design pattern that generally provides a way to create objects without specifying the exact class of the object that will be created. It involves a single method that returns a new instance of a class, often based on input parameters.


This pattern is typically used when the creation logic of the objects is not simple, but the types of objects are not numerous and do not form a hierarchy. Otherwise, this variation of the design pattern may not hold up as well.

Diving into the Code

In this example, let’s say you’re trying to create a report generator and you create a factory called ReportGeneratorFactory that employs an interface called IReportGenerator. Thus, all report generator classes must implement the IReportGenerator interface and set up any specific types of constructors required.

public interface IReportGenerator
{
    string GenerateReport();
}

public class XmlReportGenerator : IReportGenerator
{
    public string GenerateReport()
    {
        return "This is an generated Xml Report.";
    }
}

public class CsvReportGenerator : IReportGenerator
{    
    public string GenerateReport()
    {
        return "This is an generated Csv Report.";
    }
}

public class ReportGeneratorFactory
{
    public IReportGenerator GetReportGenerator(string format)
    {
        switch (format.ToLower())
        {
            case "xml":
                return new XmlReportGenerator();
            case "csv":
                return new CsvReportGenerator();
            default:
                throw new ApplicationException("Report format not supported.");
        }
    }
}

In this example, the ReportGeneratorFactory class provides different report generators based on the client’s input, where objects are generated through the GetReportGenerator method.


Building a Complex Factory Pattern in C#

Another way to implement the Factory pattern is to use the Abstract Factory pattern. The Abstract Factory pattern can create factories on two levels to supply objects in more objectively complex scenarios. This pattern is used when the system needs to be independent of how its products are created, composed, and represented, and when the family of related product objects is designed to be used together.


Here are the steps to building a complex Factory pattern:

  1. Create an abstract Factory class that all factories at the application level will implement.
  2. In the abstract Factory class, you can define a method that creates application-specific factories.
  3. Each application-specific factory will have its interface that will be used to create each class.

Diving into the Code

Here is an example of implementing the Abstract Factory pattern in C#:

public abstract class Factory
{
    public abstract ILiteratureFactory MakeLiteratureFactory();
}

public interface IBook
{
    string GetGenre();
}

public class FictionBook : IBook
{
    public string GetGenre()
    {
        return "Fiction Book.";
    }
}

public class NonFictionBook : IBook
{
    public string GetGenre()
    {
        return "Non-Fiction Book.";
    }
}

public interface ILiteratureFactory
{
    IBook CreateBook();
}

public class FictionFactory : ILiteratureFactory
{
    public IBook CreateBook()
    {
        return new FictionBook();
    }
}

public class NonFictionFactory : ILiteratureFactory
{
    public IBook CreateBook()
    {
        return new NonFictionBook();
    }
}

public class LiteratureAcademy: Factory
{
    public override ILiteratureFactory MakeLiteratureFactory()
    {
        return new FictionFactory();
    }
}

In this example, the program has three classes: FictionBook, NonFictionBook and LiteratureAcademy. These classes utilize the Abstract Factory pattern by inheriting the abstract Factory class. The FictionBook and NonFictionBook classes, being classes that require a literature factory to be instantiated, implemented the IBook interface. The LiteratureAcademy class implemented the MakeLiteratureFactory()method from the Factory class, and the FictionFactory and NonFictionFactory classes inherit from the abstract ILiteratureFactory.


This model allows the application to create different items while remaining behind the application-level object factory, providing sufficient object abstractions. Furthermore, this approach promotes simple yet efficient unit testing while offering maintainable and scalable solutions.


How to Use the Factory Pattern in C#

The Factory pattern provides developers with an alternative way to create object instances without relying on constructors or class implementations. In C#, the Factory pattern can be used to build robust and scalable software solutions. Here, we will look into how to use the Factory pattern in your C# codebase.


When using the Factory pattern, you need to define a Factory class that has a common interface or abstract class to create and return objects of the same type. The Factory class can also be extended to include additional logic for creating object instances based on runtime concerns such as licensing, features, platform, etc.

Best Practices When Using the Factory Pattern in C#

When using the Factory pattern in C#, there are some best practices you should keep in mind. Here are some examples:

  • Use the Factory class to prevent objects from being created directly by construction.

  • Always define an interface or abstract class to be implemented by the object classes.

  • Use an enumeration or a configuration file to define the Factory input parameters. Consider if an enum makes sense!


Also keep in mind that if your factory has an interface and the return type is an interface, this can be especially helpful when it comes to unit testing. If your system under test needs to create new objects as part of the path of execution, a mock can intercept this and return a newly mocked object.

Common Mistakes to Avoid When Using the Factory Pattern in C#

Even though the Factory pattern is a well-known design pattern, there are still some common mistakes that developers can make when using it in C#. Here are some of the things that developers should avoid:

To avoid these situations, you should try to follow some of the best practices when implementing the Factory pattern in C#. Proper input validation and testing should be done to ensure the code is working as intended. But you knew this already… we LIKE testing our code! By using the Factory pattern correctly, developers can create robust and scalable software solutions.


Wrapping up Examples of the Factory Pattern in C#

In conclusion, the Factory pattern is a useful design pattern for creating software solutions in C#, especially with complex object creation. By using the Factory pattern, developers can improve the flexibility, maintainability, and testability of their codebases.

Some of the benefits of using the Factory pattern in C# include:

  • Encapsulation of object creation logic

  • Decoupling of client code and object creation code

  • Increased flexibility in object creation


Implementing the Factory pattern in C# not only improves software development in terms of coding efficiency and organization but can also help create more reliable software systems. Remember that not every situation needs a factory pattern, so try and find the sweet spot for where it adds value!


If you enjoyed this article, don’t forget to subscribe to my newsletter, Dev Leader Weekly, and follow my YouTube channel for more tips and tricks related to C# and software development. If you’re interested in more learning opportunities, subscribe to my free weekly newsletter and check out my YouTube channel!


Frequently Asked Questions: Examples of the Factory Pattern in C#

What are examples of the factory pattern in C#?

Examples of the factory pattern in C# include the creation of objects based on a specified set of parameters. For example, creating different types of vehicles such as cars, trucks, and motorcycles using a VehicleFactory. Another example is creating different types of database connections using a DatabaseFactory.

What are the benefits of using the factory pattern in C#?

The benefits of using the factory pattern in C# include improved code organization and extensibility, increased code reusability, and simplified object creation. It also helps to reduce dependencies between objects and improve overall system flexibility.

How do you build a basic factory pattern in C#?

To build a basic factory pattern in C#, you need to create a factory class that generates instances of the desired object based on user input. The user input can be a string, an enumeration, or other data type. Then, use the factory method to create the desired object. An example of a basic factory pattern in C# is creating a ShapeFactory that generates different shapes like circles, squares, and triangles. The factory would take in a parameter that tells is what kind of shape to make, and the return value would be the shape.

How do you use the factory pattern in C#?

To use the factory pattern in C#, you need to create a factory class that generates instances of objects based on user input. Then, call the factory method to create the desired object. You can also use the factory pattern with other design patterns such as the Singleton pattern, Repository pattern, or Strategy pattern. Additionally, it is important to follow best practices when using the factory pattern such as using abstractions instead of concrete classes, abstracting the creation process in a factory interface or abstract class, and using dependency injection to inject the factory class as a dependency.

What are common mistakes to avoid when using the factory pattern in C#?

Common mistakes to avoid when using the factory pattern in C# include creating unnecessary complexity, violating the open-closed principle, and not using abstractions. It is also important to avoid hard-coding dependencies, not adhering to naming conventions, and not handling exceptions or errors properly.


Also appears here.