Factory Design Pattern and Abstract Factory Design Pattern in C#

Factory design pattern and Abstract Factory design patterns are Creational or Construction design patterns.

Before going to discuss about Factory design patterns, first we have to know something about design patterns.

What is design pattern ?
Design pattern is a general reusable solution to a commonly occurring problem in software design.

Design patterns are language independent mechanisms for providing solutions to common problems. Adhering to design patterns help in creating systems that are robust, scalable and maintainable as design patterns follow best practices and established standards. Design patterns have existed in non software engineering disciplines for a long time. Mechanical engineers reuse proven standard design models when designing a new kind of lathe machine. Instead, they reuse existing designs.

First we will discuss about Factory design pattern in C#.

Factory Design Pattern:
Generally when we develop a class, we usally provide the class constructors to let clients of our class instantiate it. But sometimes client needs an object that does not know which of several possible classes to instantiate. In this case Factory design pattern helps us.

Factory design pattern lets a class developer define the interface for creating an object while retaining control of which class to instantiate.

By using Factory method pattern, you can isolate the client from knowing which class instantiate.



Here I am explaining the factory design pattern with online payment gateway example.

For example you are selling some products online and you provide the payment gateway for you users to pay the money. You have two bank accounts. You can use one of your two bankers depending on the availability of the banks.

Factory method design pattern has minimum of following components.

IBank – Interface for Bankers
BankA, BankB – Banker classes that implement the IBank
Bank – class Provides AvailableBankMethod
AvailableBankMethod – provides which Bank class has to instantiate.

 
First we will create IBank interface with Pay() method.

 

interface IBank
{
     void Pay();
}

 

Next, we will create Banker classes which implements IBank interface.

 

class BankA:IBank
{
      public void Pay()
      {
            Console.WriteLine("This is From Bank A");
      }
}
 

class BankB:IBank
{
      public void Pay()
      {
           Console.WriteLine("This is From Bank B");
      }      

}

Here, we are displaying the message for Pay() method. You can have your own functionality.

Next we will create Bank class through which client will interacts actual Banker classes.

 

class BankFactory
{
        public static IBank AvailableBankMethod()
        {
            //For even second we are returning Bank A and for odd second Bank B.
        //You can have your own logic to decide which bank available
            int scnd = DateTime.Now.Second;

            if (scnd%2 == 0 )
            {
                return new BankA();
            }
            else if(scnd % 2 == 1)
            {
                return new BankB();
            }
            else
            {
                return null;
        }
        }   
}

 

In the above code, for even second we are returning Bank A and for odd second Bank B. You can have your own logic to decide which bank available.

Now we will create some sample code how client will makes calls in Factory design pattern.

static void Main(string[] args)
{
             //Client is using only Bank class and IBank Interface instead of
         //individual Bank classes to check availability

            IBank objBank;

            objBank = BankFactory.AvailableBankMethod();
            objBank.Pay();

            Console.ReadLine();
}
 

If you observe the above code, you can find that we are using IBank interface and Bank class to check availability of Bank instead of calling the each Bank to check availability.

 

From the above example you can observe that, Factory design pattern has several advantages.

 

  • By using Factory design pattern, you can hide the concrete classes and it’s methods, functionality from the client.
  • You can avoid number of "new" keywords in client code by calling Factory class instead of each class separately.
  • If you want add one more Bank to your payment gateway, you can easily add it to your code with out knowing to your client and without code change at client side.


You can download source code for above example from below links.

 

                                                                    Download Source Code For Factory Design Pattern