top of page
Writer's pictureThe Tech Platform

Factory Design Pattern (C#)

Updated: Apr 5, 2023

The Factory Design Pattern is a creational pattern that provides an interface for creating objects in a super-class but allows subclasses to alter the type of objects that will be created. This pattern provides a way to encapsulate the creation logic of an object, and it is widely used in modern software development. In this article, we will explore the Factory Design Pattern using C# code examples.


Overview of the Factory Design Pattern

The Factory Design Pattern is based on the idea of defining an interface for creating objects but letting subclasses decide which classes to instantiate. The factory pattern is used to replace object instantiation with an interface that defines the classes to be instantiated.


To implement the Factory Design Pattern, we need to define three things:

  1. A base abstract class or interface for the products.

  2. Concrete classes that implement the products.

  3. A factory class that returns the concrete classes based on the request.

In C#, we can implement the Factory Design Pattern using the following steps:

Step 1: Define an interface or abstract class for the products

We begin by defining an interface or abstract class for the products. This interface will define the methods that each product class must implement. For example:

public interface IProduct {     void Operation(); } 

Step 2: Define concrete classes that implement the products

Next, we define concrete classes that implement the products. Each concrete class should implement the IProduct interface. For example:

public class ConcreteProductA : IProduct 
{     
    public void Operation()
    {         
        Console.WriteLine("ConcreteProductA.Operation()");     
    } 
}  

public class ConcreteProductB : IProduct 
{     
    public void Operation()
    {         
        Console.WriteLine("ConcreteProductB.Operation()");     
    } 
} 

Step 3: Define a factory class that returns the concrete classes based on the request

Finally, we define a factory class that returns the concrete classes based on the request. This factory class should contain a method that takes a parameter, and based on that parameter, it should return an instance of the appropriate concrete class. For example:

public class Factory 
{     
    public IProduct CreateProduct(string type)     
    {         
        if (type == "A")             
            return new ConcreteProductA();         
        else if (type == "B")             
            return new ConcreteProductB();         
        else
            throw new ArgumentException("Invalid type.", "type");     
     } 
} 

Usage of the Factory Design Pattern

To use the Factory Design Pattern, we need to create an instance of the factory class and call its CreateProduct() method to get an instance of the desired product. For example:

Factory factory = new Factory();  

IProduct productA = factory.CreateProduct("A"); 
productA.Operation(); // Output: ConcreteProductA.Operation()

IProduct productB = factory.CreateProduct("B"); 
productB.Operation(); // Output: ConcreteProductB.Operation()

There are lots of limitations, such as:

  1. The name of the constructor has to match the name of the containing type, so you cannot communicate additional information that how the object is constructed.

  2. Cannot overload constructor with the same sets of arguments with different names

  3. Object Creation (Non — piecewise, unlike Builder) can be outsourced to (A separate function( Factory Method)).

  4. That may exist in a separate class (Factory)

  5. Can create a hierarchy of factories with Abstract Factory

Conclusion

In this article, we have learned about the Factory Design Pattern and how it can be implemented using C#. The Factory Design Pattern is a powerful tool for creating flexible and maintainable code. By encapsulating the creation logic of an object into a factory class, we can easily change the type of objects that will be created without affecting the rest of the code. This makes it easier to maintain and extend the code over time.

0 comments

Recent Posts

See All

Comments


bottom of page