Factory pattern in C#


Note: If you are new to design pattern please look the part 1 of Design Pattern

https://chandradev819.wordpress.com/2015/12/17/what-is-the-design-pattern/

Factory pattern is creational design pattern in C#. As Factory name suggest that there should be something new create and construct.

In software architecture world factory pattern means centralize creation of object.

Now let take a scenario, we are working on some application and we got the scenario to generate the invoice on basis of City wise. Then generally we will write the two methods for generating the invoice and we will create the object with new keyword in UI layer.

Now we can optimize this code using factory design pattern. If we are using factory design pattern then no need create object in UI layer using New keyword. We can create factory class which will handle this task.

Now i am going to show extremely basic demo sample code for factory design pattern.

using System;

namespace factorypattern_example
{

    public class Program
    {
        public static void Main(string[] args)
        {
            Iinvoice objInvoice;
            Console.WriteLine("Enter the area Code");
            int invoiceType = Convert.ToInt32(Console.ReadLine());
            objInvoice = FactoryInvoive.GetInvoice(invoiceType);
            objInvoice.print();
            Console.ReadLine();
        }

    }

    /// <summary>
    /// Here we have created interface for common functionality
    /// </summary>
    public interface Iinvoice
    {
        void print();
    }

    /// <summary>
    /// Here we have implemented interface for bangalore branch
    /// </summary>
    public class ProductBangalore : Iinvoice
    {
        public void print()
        {
            //Note: this the demo code 
            Console.WriteLine("Invoice will be printed for Bangalore product");
        }
    }

    /// <summary>
    /// Here we have implemented interface for Mumbai branch 
    /// </summary>
    public class ProductMumbai : Iinvoice
    {
        public void print()
        {
            Console.WriteLine("Invoice will be printed for Mumbai product");
        }
    }

    /// <summary>
    /// Here we are creating the factory, which will take care of object creation task.
    /// </summary>
    public class FactoryInvoive
    {
        static public Iinvoice GetInvoice(int CityCode)
        {
            Iinvoice objInv;
            if(CityCode==100)
            {
                objInv = new ProductBangalore();

            }
           else if(CityCode==101)
            {
                objInv = new ProductMumbai();
            }
            else
            {
                return null;
            }
            return objInv;
        }
    }
}

Summary

If you observed in above code, in main method we are not creating object of class and factoryInvoice class has been created to handle the object creation task so this pattern is called as Factory Design Pattern.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.