Factory Method Design Pattern in C#

Factory method design pattern is one of the most used design pattern in .NET. In this article, I will explain about how to implement factory method design pattern in C#.

Factory method design pattern comes under creational design pattern category of Gang of Four (GoF) design patterns. Basically, it solves the problem related to object creation.

First we will see what problem does factory pattern solves then we will see about how to implement factory pattern.

What problem Factory pattern solves?

Let’s see an example code that generates various objects:

The above example generates objects of different classes after checking a condition. But the above code is having two issues:

  • Lot of new keyword: The above code implements lot of new keyword which increases your client code complexity and also increases maintenance requirement. If you will add one more condition, then one more new keyword will be required.
  • Client will know all the concrete objects types; if it is required to instantiate one more class then client should be required to know that class too.

Factory pattern provides solution for above mentioned problems. It helps you to create objects in a different way.

Factory Method Design Pattern

As the name Factory indicates that it will create something. This something is nothing but the objects. Factory method design pattern is a way to create object but the client or calling class will not know about how the objects were created. In factory pattern subclass decides about instantiation of a class.

Gang of Four (GoF) definition:

Define an interface for creating an object, but let subclasses decide which class to instantiate.

UML Class Diagram

  • Client will know only about the interface of Product not its concrete implementation.
  • Client will declare product variable of IProduct type, but calls Creator.FactoryMethod to instantiate it.

Factory method designpattern in C# - UML diagram

 

The participants in above UML diagram are:

IProduct: The interface for the concrete product

ConcreteProduct: Class type that provides Concrete implementation of IProduct interface

Creator: Abstract class that provides FactoryMethod.

ConcreteCreator: Class that will implement abstract method FactoryMethod

FactoryMethod: Method that will decide which class will instantiate

Factory method design pattern in C# – Example implementation

To understand factory method design pattern in C#, I will explain the same example that I have discussed in the start of this article. The example scenario requires you to create different type of employee object for any organization such as permanent employee, temporary employee, part time employee or full time employee.

Class diagram

Factory pattern design pattern in C# - class diagram

Participants in above class diagram are:

IFactory: Interface

PerrmanentEmployee and TemporaryEmployee: Concrete Product classes

EmployeeFactory: creator

ConcreteEmployeeFactory: Concrete creator class

Implementation Code – C#

Output

Factory method design pattern in C# - result

I hope you enjoyed reading this article – factory method design pattern in C#. Post your comments for queries and feedback. Thanks!

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

8 thoughts on “Factory Method Design Pattern in C#

  • July 14, 2016 at 9:27 am
    Permalink

    Very useful to us…..
    Thnkyu sir…..

    Reply
  • July 18, 2016 at 10:39 am
    Permalink

    Very Good Article to learn..

    Reply
  • September 30, 2016 at 5:59 pm
    Permalink

    Very informative.Thanks for sharing.

    Reply
  • November 4, 2016 at 12:45 pm
    Permalink

    That is the only blog I found useful with clear example. Thanks for sharing.

    But I have a general question using real life example.

    Question: In my tenure of programming, I haven’t seen such cases where whole application needs to be implemented using Factory method. There are only few cases/modules where we require dynamic objects to be created. So what should be the best approach ? Should we apply factory pattern only for those modules or for whole application ?

    Reply
  • March 6, 2018 at 1:55 pm
    Permalink

    Hi Shekhar ,
    I can see one prob with this implementation, even though the if() condition is removed, still if we look at the EmployeeFactory here we are using switch case which will increase the maintenance.
    Can we use strategy pattern is place of switch case?

    Reply
  • November 7, 2018 at 10:17 pm
    Permalink

    Many thanks shekhar, on internet this is the best C# blog explaining about design patterns I found, keep sharing

    Reply
  • February 13, 2019 at 4:19 pm
    Permalink

    Easy to understand can you please share AbstractFactory also

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *