Click here to Skip to main content
15,400,433 members
Articles / Programming Languages / C#
Posted 2 Oct 2012

Tagged as


28 bookmarked

Illustrating Factory pattern with a very basic example

Rate me:
Please Sign up or sign in to vote.
4.80/5 (33 votes)
2 Oct 2012CPOL2 min read
A simplified explanation of Factory pattern with sample C# .NET code.


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.


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.

// customer enters their choice
If (choice == "Car")
	// call car class and it’s methods
	Car c = new car();;
If (choice == "bike")
	// call bike class and it’s methods
	Bike b = new Bike();
  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.


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.

// 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.

// 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();
            objChoice = new InvalidChoice();
        return objChoice;
//Business classes
public class clsBike:IChoice
    #region IChoice Members
    public string Buy()
        return ("You choose Bike");
public class clsCar:IChoice
    #region IChoice Members
    public string Buy()
        return ("You choose Car");

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.

//Client class
IChoice objInvoice;
objInvoice = FactoryClass.FactoryChoice.getChoiceObj(txtChoice.Text.Trim());

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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rajesh Varma Buddaraju
Team Leader
United States United States
Professional Summary -
1. I am a Microsoft Certified Professional with 14+ years of IT Professional experience with both Web and Windows Application using Microsoft Technologies/Tools such as ASP.NET using C#,WPF, VB.NET, Angular JS, ADO.NET, Entity Framework, Web Services, WCF Services, Restful Services, Java Script, XML, HTML, WPF, SSIS, LINQ, SSRS, Silver light and Expression Blend, Entity Framework, Visual Basic 6.0 & Classic ASP.

2. Results oriented Technology Professional with over 14 years of experience serving several roles as Architect, Lead Developer, Sr. Development Engineer and Team Lead in Projects.

3. Currently working as a Lead developer for a leading firm.

4. Scaled Microsoft Certified Technology Specialist with 8+ years in Agile – Scrum Methodology.

5. Involved in all phases of software development life cycle (SDLC) including analysis, requirement engineering, architecture design, database design, development, enhancements and maintenance of standalone, multi-tier, mobile apps, web-based, portals based object oriented enterprise applications.

6. Extensive experience in leading large teams and involved in project estimation, planning, scheduling and execution, status reporting, cross team co-ordination, issues and risks management.

Comments and Discussions

QuestionGood article but having a doubt Pin
Member 1107212616-Oct-17 3:45
MemberMember 1107212616-Oct-17 3:45 
GeneralMy vote of 5 Pin
Member 43208442-Jan-16 22:58
MemberMember 43208442-Jan-16 22:58 
GeneralMy vote of 5 Pin
acdilag4-Sep-15 20:42
Memberacdilag4-Sep-15 20:42 
GeneralMy vote of 1 Pin
raiserle13-May-15 0:23
Memberraiserle13-May-15 0:23 
GeneralHi Rajesh Pin
RamaDakka25-Dec-14 21:56
MemberRamaDakka25-Dec-14 21:56 
GeneralVery Nice Article for Beginners Pin
itsgkiran23-Dec-14 2:59
Memberitsgkiran23-Dec-14 2:59 
GeneralMy vote of 5 Pin
Md.Mobidul Islam(Moin)13-Oct-14 23:42
professionalMd.Mobidul Islam(Moin)13-Oct-14 23:42 
QuestionOne Question Pin
girishmeena28-Mar-14 10:34
Membergirishmeena28-Mar-14 10:34 
What are the different layer available to put all non-client code? In short where it will be good to keep all non-client code of the example?
GeneralMy Vote of 5 Pin
girishmeena22-Mar-14 10:16
Membergirishmeena22-Mar-14 10:16 
GeneralMy vote of 5 Pin
lahiru madusanka12318-Aug-13 22:50
Memberlahiru madusanka12318-Aug-13 22:50 
GeneralMy vote of 4 Pin
Mohan.dotnet19-Jun-13 3:55
MemberMohan.dotnet19-Jun-13 3:55 
GeneralMy vote of 5 Pin
Ankur11june22-Jan-13 2:34
MemberAnkur11june22-Jan-13 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.