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.
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#.
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.
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.
online pharmacy canada pharm canadian pharmacies
sex ed ed meds online pharma
best drugstore makeup best drugstore moisturizer best canadian pharmacy
mexican pharmacy online drugs from canada online pharmacy without scripts
pharma canada pharmacy nearest drugstore
erection pills usa pharmacy pharmacy open near me
The very next time I read a blog, Hopefully it won’t fail me as much as this one. After all, Yes, it was my choice to read, nonetheless I truly thought you would probably have something useful to talk about. All I hear is a bunch of whining about something you can fix if you weren’t too busy searching for attention.
https://snegad.com/qtoa/index.php?qa=69467&qa_1=%D7%90%D7%A0%D7%92%D7%9C%D7%99%D7%A0%D7%94-%D7%90%D7%A1%D7%A7%D7%95%D7%A8%D7%98-%D7%A0%D7%A2%D7%A8%D7%95%D7%AA-%D7%9C%D7%99%D7%95%D7%95%D7%99-%D7%AA%D7%9C-%D7%90%D7%91%D7%99%D7%91
The next time I read a blog, I hope that it doesn’t disappoint me as much as this particular one. After all, Yes, it was my choice to read through, but I genuinely thought you would probably have something interesting to talk about. All I hear is a bunch of crying about something that you could fix if you were not too busy seeking attention.
https://heavenenvoy.mn/%D7%A0%D7%A2%D7%A8%D7%95%D7%AA-%D7%A0%D7%A2%D7%A8%D7%95%D7%AA-%D7%9C%D7%99%D7%95%D7%95%D7%99-%D7%9C%D7%94%D7%96%D7%9E%D7%A0%D7%94-%D7%91%D7%A0%D7%AA%D7%A0%D7%99%D7%94-%D7%95%D7%90%D7%96%D7%95%D7%A8/
Hey! Someone in my Myspace group shared this site with us so I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers! Excellent blog and wonderful design.
Thank you for the auspicious writeup. It in reality was once a entertainment account it. Glance complicated to more introduced agreeable from you! By the way, how could we be in contact?|
Hi, constantly i used to check webpage posts here early in the daylight, because i love to learn more and more.|
May I just say what a comfort to uncover somebody who genuinely knows what they are discussing on the net. You certainly understand how to bring an issue to light and make it important. A lot more people ought to look at this and understand this side of your story. I was surprised that you are not more popular because you certainly possess the gift.
http://eduardoey381.blog5star.com/1976758/not-known-factual-statements-about-?????-?????????
Can I just say what a comfort to find a person that actually understands what they’re discussing on the web. You definitely know how to bring an issue to light and make it important. More people ought to look at this and understand this side of your story. I can’t believe you are not more popular given that you most certainly have the gift.
http://israel470pd.slypage.com/2228692/facts-about-?????-????????-revealed
May I just say what a comfort to discover someone who genuinely knows what they’re talking about over the internet. You definitely understand how to bring a problem to light and make it important. More and more people have to check this out and understand this side of the story. It’s surprising you’re not more popular because you surely have the gift.
http://petero925bpc4.topbloghub.com/profile