Factory Design Pattern in C#

 

Factory design pattern is very useful when you want to avoid the business logic from client. That means by using factory design pattern you can avoid the creating object from client side. In this article we discuss about Factory Design Pattern in C#. 

For example we have web service which provides different motor vehicles based on fuel, we have petrol and diesel version vehicles. Based on client input we will provide the vehicles details. So we have two classes, one for petrol and another for diesel version. If we provide the access for these classes we have below issues. 

·         Client has to create separate object for two classes that means client has to use new keyword two times. If number of class’s increases we have to use more new keywords. 

·         Client knows like there are two classes, that means we are exposing business logic to client 

·         In future if we add the new fuel version vehicle class, client has to change his code again to get these vehicles list. 

By using factory design pattern we can avoid all these issues. In C# factory design pattern, one factory class will reside between client and actual business logic. This factory class will decide which product class object we have to return to client based on its input. 

First we have one interface IVehicles which has GetVehicles() method. All product classes has to implement this interface to implement GetVehicles() method. As shown below we have to create IVehicles interface and two product classes called Petrol, Diesel which are implementing the IVehicles interface. 

IVehicles.cs 

namespace FactoryDesignPatternCSharp 

{ 

    public interface IVehicles 

    { 

        string GetVehicles(); 

    } 

}

 

Petrol.cs 

using System;

 

namespace FactoryDesignPatternCSharp 

{ 

    class Petrol: IVehicles 

    { 

        #region IVehicles Members

 

        public string GetVehicles() 

        { 

            return "Hyundai Grand i10" + Environment.NewLine + "Ford Ecosport" + Environment.NewLine + "Honda Amaze" + Environment.NewLine + "Nissan Terrano"; 

        }

 

        #endregion 

    } 

}

 

Diesel.cs 

using System;

 

namespace FactoryDesignPatternCSharp 

{ 

    class Diesel: IVehicles 

    { 

        #region IVehicles Members

 

        public string GetVehicles() 

        { 

            return "Ford Fiesta" + Environment.NewLine + "Volvo XC60" + Environment.NewLine + "Hyundai i20" + Environment.NewLine + "Chevrolet Captiva"; 

        }

 

        #endregion 

    } 

}

 

Now instead of exposing these classes directly to the client we will have one more class called FactoryVehicles.cs which decides which object we have to return to client based on its input as shown below. 

namespace FactoryDesignPatternCSharp 

{ 

    public class FactoryVehicles 

    { 

        static public IVehicles GetVehicles(string sFuel) 

        { 

            IVehicles iVehicles = null; 

            if (sFuel.ToLower().Trim() == "petrol") 

            { 

                iVehicles = new Petrol(); 

            } 

            else if (sFuel.ToLower().Trim() == "diesel") 

            { 

                iVehicles = new Diesel(); 

            } 

            return iVehicles; 

        } 

    } 

}

 

As shown above in the FactoryVehicles class has GetVehicles() method which takes input from client and based on input it returns object. Now we will call this Factory class from client as shown below. 

using System; 

using System.Windows.Forms;

 

namespace FactoryDesignPatternCSharp 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            IVehicles objVehicles; 

            objVehicles = FactoryVehicles.GetVehicles(txtFuel.Text); 

            MessageBox.Show(objVehicles.GetVehicles());  

        } 

    } 

}

 

As shown above we are exposing FactoryVehicles class to client and based on client input we are returning the product object without client knowledge.

In future if we want to provide new fuel vehicles just we need to add new fuel class to service and we have to change factory class FactoryVehicles, client no need to change any logic. 

Factory Design Pattern Advantages: 

·         We can avoid creating object from client side. So by using Factory Design pattern we can avoid using new keyword in client side. 

·         We can avoid client to know the actual business logic. 

·         In future if we want to add new Product class, we can easily add the new class without chaining the client code.

                                                                                        FactoryDesignPatternCSharp.zip (44.20 kb)