In this tutorial, I am going to explain you about Factory Design Pattern. Also, we will go through with a simple example in C#.

What is Factory Design Pattern?

As the name sounds t makes and constructs something. Factory Design Pattern is a design pattern that comes under Creational Pattern of Gang of Four (GOF) design pattern .net. It simply provides a better way to create the object. The main idea behind Factory Design is to simply the object creation. With Factory Design Patterns we create an object of the class without exposing the instantiation logic to the client and referring to the newly created object through the interface.

Factory Design Pattern

Why we need Factory Design Pattern?

The main aspect of every application is to create the object. We create a tonne of the object that we use as a part of our application or framework. The complexity of application increases as our application grows. It becomes difficult to manage application with each creation of the object. When we create more and more objects, we start using flags to control which class will be instantiated and used. Let’s understand with a block of code.

Suppose an application has many roles and for each Role, there are different configuration settings. So the code will be.

String sRole;

If(Role==”Admin”)

{

LoadAdminConfiguration objAdminConfiguration=new LoadAdminConfiguration();

}

else If(Role==”Manager”)

{

LoadManagerConfiguration objManagerConfiguration=new LoadManagerConfiguration();




}

else  If(Role==”Accountant”)

{

LoadAccountantConfiguration objAccountantConfiguration=new LoadAccountConfiguration();




}

In above code, our basic requirement is to load the configuration for different types of roles. So the two major problems with the above approach are:

  • We have a lot of scattered new keyword for each role. If there is one more Role there will be one more new keyword. With each new keyword, your code complexity increases drastically.
  • Another major issue related to this approach is the client is aware of each Role this means that the client is aware of there is LoadAdminConfiguration class for Admin Role and there is a class called LoadManagerConfiguration for Role Manager. Another thing that comes in my mind that if we will add another Role in future we cannot add new Role without informing to the client.

Simple C# code example for Factory Design Pattern.

Step 1- Open Visual Studio and create a Console Application with C#.

Factory Design Pattern

Step 2- Right Click on the Solution Explorer and Create a Parent class with the name LoadConfigurationSettings. Write the following code in LoadConfigurationSettings.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDessignPatternDemo
{
 class LoadConfigurationSettings
 {
 protected string sRole;
 public string GetRole()
 {
 return sRole;

}
 }
}

Step 3- Now we need to create three child classes Admin, Manager and Account and inherit each class with LoadConfigurationSettings.

Code for Admin Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDessignPatternDemo
{
 class Admin:LoadConfigurationSettings
 {
 public Admin()
 {
 sRole = "Admin";
 Console.WriteLine("Loading Configuration Setting for : {0}", sRole);

}
 }
}

Code for Manager Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDessignPatternDemo
{
 class Manager:LoadConfigurationSettings
 {
 public Manager()
 {
 sRole = "Manager";
 Console.WriteLine("Loading Configuration Setting for : {0}", sRole);

}
 }
}

Code for Accountant Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDessignPatternDemo
{
 class Accountant:LoadConfigurationSettings
 {
 public Accountant()
 {
 sRole = "Accountant";
 Console.WriteLine("Loading Configuration Setting for : {0}", sRole);

}
 }
}

Building Factory Class:

Step 4- Right Click on the Solution Explorer and create a class with the name ConfigurationFactory, Write the following code in ConfigurationFactory.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDessignPatternDemo
{
 class ConfigurationFactory
 {
 public LoadConfigurationSettings ChoseRole(int iType)
 {
 switch (iType)
 {
 case 1:
 return new Admin();
 case 2:
 return new Manager();
 default:
 return new Accountant();
 }
 }
 }
}

Using the Factory Class in Main Class:

Step 5- Open Program.cs file and  write the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDessignPatternDemo
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine("Type 1 for Admin Configuration:");
 Console.WriteLine("Type 2 for Manager Configuration:");
 Console.WriteLine("Type any number for Accountant Configuration:");
 string input = Console.ReadLine();
 ConfigurationFactory objConfigurationFactory = new ConfigurationFactory();
 LoadConfigurationSettings objConfigurationSetting = objConfigurationFactory.ChoseRole(int.Parse(input));
 Console.WriteLine("From main class " + objConfigurationSetting.GetRole());
 Console.Read();
 }
 }
}

Run the Application and enter 1,2 or any key. You can see the output in the below snapshot.

Factory Design Pattern

You can download the complete source code for the application from here.

You must also follow the related links:

Hope you loved this post about Factory Design Pattern in C#.

Thank You.

Leave a Comment