Factory pattern C#

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Introduction

The factory pattern method is a popularly used design pattern and it is very useful to restrict clients from knowing the actual business logic methods, it is useful to create a decoupled system, and it is useful to eliminate object creation in a client environment.

This article explains how we can achieve these problems with a small example.

Problem

For example we have a scenario where based on user input we need to call particular class methods. I have a customer input screen. He enters his choice whether they want to buy a bike or a car. Normally we get input from the user and based on that will create an object in the client class and call those methods like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// customer enters their choice
//
If (choice == “Car”)
{
    // call car class and it’s methods
    Car c = new car();
    c.buy();
}
If (choice == “bike”)
{
    // call bike class and it’s methods
    Bike b = new Bike();
    b.buy()
}
//
// customer enters their choice
//
If (choice == “Car”)
{
	// call car class and it’s methods
	Car c = new car();
	c.buy();
}
If (choice == “bike”)
{
	// call bike class and it’s methods
	Bike b = new Bike();
	b.buy()
}
  1. In case in future if there is any other vehicle added then we need to change the client functionality.
  2. The above client code depicts that there are classes Car, Bike, and a method Buy. There is no security at the client side.
  3. Need to use the new keyword in client classes.

To avoid these problems Factory pattern is used.

Solution

Problem 1

Create a new interface to depict methods, for example, in our scenario, it is Buy(). Using this interface it will solve problems 1 and 2.

1
2
3
4
5
6
7
//
// Interface
//
public interface IChoice
{
    string Buy();
}
//
// Interface
//
public interface IChoice
{
    string Buy();
}

A new class will be added and this class we will be called factory class. This class sits between the client class and the business class and based on user choice it will return the respective class object through the interface. It will solve problem 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Factory Class
//
public class FactoryChoice
{
    static public IChoice getChoiceObj(string cChoice)
     {
        IChoice objChoice=null;
        if (cChoice.ToLower() == "car")
        {
            objChoice = new clsCar();
        }
        else if (cChoice.ToLower() == "bike")
        {
            objChoice = new clsBike();
        }
        else
        {
            objChoice = new InvalidChoice();
        }
        return objChoice;
    }
}
 
//Business classes
//Car
public class clsBike:IChoice
{
    #region IChoice Members
 
    public string Buy()
    {
        return ("You choose Bike");
    }
 
    #endregion
}
 
//Bike
public class clsCar:IChoice
{
 
    #region IChoice Members
 
    public string Buy()
    {
        return ("You choose Car");
    }
 
    #endregion
}
//
// Factory Class
//
public class FactoryChoice
{
    static public IChoice getChoiceObj(string cChoice)
     {
        IChoice objChoice=null;
        if (cChoice.ToLower() == "car")
        {
            objChoice = new clsCar();
        }
        else if (cChoice.ToLower() == "bike")
        {
            objChoice = new clsBike();
        }
        else
        {
            objChoice = new InvalidChoice();
        }
        return objChoice;
    }
}

//Business classes
//Car
public class clsBike:IChoice
{
    #region IChoice Members

    public string Buy()
    {
        return ("You choose Bike");
    }

    #endregion
}

//Bike
public class clsCar:IChoice
{

    #region IChoice Members

    public string Buy()
    {
        return ("You choose Car");
    }

    #endregion
}

From the client class call the factory class object and it will return the interface object. Through the interface object we will call the respective method.

1
2
3
4
//Client class
IChoice objInvoice;
objInvoice = FactoryClass.FactoryChoice.getChoiceObj(txtChoice.Text.Trim());
MessageBox.Show(objInvoice.Buy());
//Client class
IChoice objInvoice;
objInvoice = FactoryClass.FactoryChoice.getChoiceObj(txtChoice.Text.Trim());
MessageBox.Show(objInvoice.Buy());

In future if we need to add a new vehicle then there is no need to change the client class, simply return that object using the factory class.

Advantages

Suppose we want to add a decoupled structure and don’t want to disturb the client environment again and again, this pattern is very useful. The factory class will take all the burden of object creations.


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

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