Click here to Skip to main content
13,833,015 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 2 Mar 2014
Licenced CPOL

Factory Pattern Example in C#

, 2 Mar 2014
Rate this:
Please Sign up or sign in to vote.
This tip is about how to implement factory design pattern


This tip is about how to implement factory design pattern.


Design patterns are general reusable solutions to common problems that occurred in software designing. There are broadly 3 categories of design patterns, i.e., Creational, Behavioral and Structural.

Now, Factory Design Pattern falls under the category of creational design pattern.
It deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The essence of this pattern is to "Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to sub classes."

(Source: Factory method pattern[^])

Where to Use?

It would be tedious when the client needs to specify the class name while creating the objects. So, to resolve this problem, we can use Factory pattern. It provides the client a simple way to create the object. The example below will elaborate the factory pattern in detail.

Using the Code

Here is the step by step procedure to create an application with Factory Pattern:

  1. Create a new Windows project (say: ProFactoryPattern).
  2. Add a ComboBox (name it cmbSelect) and one Label (for displaying the Result (name it lblResult)) as shown below:

  3. Add the below interface and two classes implement this interface. Please note that both classes have the same methods.
    interface IGet
        string ConC(string s1, string s2);
    class clsFirst : IGet
        public string ConC(string s1, string s2)
            string Final = "From First: " + s1+" and " + s2;
            return Final;
    class clsSecond : IGet
        public string ConC(string s1, string s2)
            string Final = "From Second: " + s1 + " and " + s2;
            return Final;
  4. Next, add the factory class, in which conditions are there to create the objects. Method in this class basically decides object of class to be created, as shown below:
    class clsFactory
        static public IGet CreateandReturnObj(int cChoice)
            IGet ObjSelector = null;
            switch (cChoice)
                case 1:
                    ObjSelector = new clsFirst();
                case 2:
                    ObjSelector = new clsSecond();
                    ObjSelector = new clsFirst();
            return ObjSelector;
  5. Finally, the client code looks like shown below. In this, the client does not bother about the classes and class name and does not worry if any new class will be added:
    private void cmbSelect_SelectedIndexChanged(object sender, EventArgs e)
        IGet ObjIntrface = null;
        ObjIntrface = clsFactory.CreateandReturnObj(cmbSelect.SelectedIndex + 1);
        string res = ObjIntrface.ConC("First", "Second");
        lblResult.Text = res;
  6. Now the output will be:

    If 1 will be selected, then object of clsFirst will be created and in case of 2, clsSecond will be created:

Points of Interest

Now consider, if we have to add one more class (say: clsThird) and we want to add one more case in switch condition (under class: CreateandReturnObj), then client code will not change. Also, you can get the input from config.


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


About the Author

Software Developer (Senior)
India India
Linkedin profile:

You may also be interested in...

Comments and Discussions

GeneralEssence used can misguide people Pin
v2.4ever16-Apr-18 22:32
groupv2.4ever16-Apr-18 22:32 
QuestionNice article for newbie's Pin
VinayR@J27-May-16 1:35
professionalVinayR@J27-May-16 1:35 
AnswerRe: Nice article for newbie's Pin
Vipin_Arora28-May-16 22:12
memberVipin_Arora28-May-16 22:12 
PraiseThanks for the example Pin
colbrand13-Mar-16 20:40
membercolbrand13-Mar-16 20:40 
QuestionAbtract factory Pin
Member 330671019-Oct-15 4:56
memberMember 330671019-Oct-15 4:56 
AnswerRe: Abtract factory Pin
Vipin_Arora25-Oct-15 21:33
memberVipin_Arora25-Oct-15 21:33 
GeneralMy vote of 1 Pin
Aditya Magotra27-Dec-14 5:37
memberAditya Magotra27-Dec-14 5:37 
GeneralRe: My vote of 1 Pin
Vipin_Arora27-Dec-14 6:33
memberVipin_Arora27-Dec-14 6:33 
GeneralRe: My vote of 1 Pin
PIEBALDconsult27-Dec-14 6:44
protectorPIEBALDconsult27-Dec-14 6:44 
GeneralRe: My vote of 1 Pin
Vipin_Arora27-Dec-14 6:58
memberVipin_Arora27-Dec-14 6:58 
GeneralRe: My vote of 1 Pin
Aditya Magotra27-Dec-14 8:42
memberAditya Magotra27-Dec-14 8:42 
GeneralRe: My vote of 1 Pin
Vipin_Arora27-Dec-14 18:49
memberVipin_Arora27-Dec-14 18:49 
GeneralRe: My vote of 1 Pin
Member 1096340329-Jun-15 22:49
memberMember 1096340329-Jun-15 22:49 
QuestionViolation of Solids (OCP) Pin
Aditya Magotra27-Dec-14 5:21
memberAditya Magotra27-Dec-14 5:21 
AnswerMessage Closed Pin
27-Dec-14 5:23
memberVipin_Arora27-Dec-14 5:23 
GeneralRe: Violation of Solids (OCP) Pin
Aditya Magotra27-Dec-14 5:31
memberAditya Magotra27-Dec-14 5:31 
GeneralMy vote of 1 Pin
PaperTape3-Dec-14 15:10
professionalPaperTape3-Dec-14 15:10 
GeneralRe: My vote of 1 Pin
Vipin_Arora3-Dec-14 22:42
memberVipin_Arora3-Dec-14 22:42 
GeneralRe: My vote of 1 Pin
PIEBALDconsult27-Dec-14 6:47
protectorPIEBALDconsult27-Dec-14 6:47 
GeneralRe: My vote of 1 Pin
Vipin_Arora27-Dec-14 6:56
memberVipin_Arora27-Dec-14 6:56 
GeneralMy vote of 4 Pin
livewire#138-Sep-14 17:52
memberlivewire#138-Sep-14 17:52 
GeneralRe: My vote of 4 Pin
Vipin_Arora29-Apr-15 20:47
memberVipin_Arora29-Apr-15 20:47 
GeneralMessage Closed Pin
3-Mar-14 2:23
professionalPBGuy3-Mar-14 2:23 
GeneralRe: My vote of 1 Pin
Klaus Luedenscheidt3-Mar-14 20:39
memberKlaus Luedenscheidt3-Mar-14 20:39 
GeneralRe: My vote of 1 Pin
Vipin_Arora3-Mar-14 22:08
memberVipin_Arora3-Mar-14 22:08 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 3 Mar 2014
Article Copyright 2014 by Vipin_Arora
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid