Click here to Skip to main content
15,867,324 members
Articles / Programming Languages / C#
Tip/Trick

Abstract Factory Design Pattern in C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (20 votes)
10 Mar 2014CPOL1 min read 86.1K   19   18
Abstract Factory Design pattern example in C#

Introduction

This tip is about to implement the Abstract Factory Pattern in C#

Background

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.
Abstract factory pattern comes under the category of creational design patterns. It basically provides a way/interface for creating groups of object or you can say families of object which are inter-dependent or of similar types. Also, for creating this families of objects, we do not requires the class name. So Abstract factory defination say "Provide an interface for creating families of related or dependent objects without specifying their concrete classes." Source: Abstract Factory

Using the code

1. Consider the below code, here two interfaces having the one method each i.e. DisplayBase() and DisplayTop()

C#
interface INormal
   {
        string DisplayBase();
   }

   interface IAboveNormal
   {
       string DisplayTop();
   }
2. Here are some concrete classes which are implementing the methods of above interfaces.
C#
public class clsWagonR : INormal
   {
       public string DisplayBase()
       {
           return "WagonR";
       }

   }

   public class clsSwift : IAboveNormal
   {
       public string DisplayTop()
       {
           return "Swift";
       }

   }

   public class clsAudiA4 : INormal
   {
       public string DisplayBase()
       {
           return "Audi A4";
       }

   }

   public class clsAudiQ7 : IAboveNormal
   {
       public string DisplayTop()
       {
           return "Audi Q7";
       }

   }
3. Now, We will create the abstract factory patter. In below code there is an interface which clubs the above interface i.e. INormal and IAboveNormal. Also see the classes which are implementing this interface.
C#
interface Icar
{
    INormal GetNormal();
    IAboveNormal GetAboveNormal();
}

class clsMaruti : Icar
{
    public INormal GetNormal()
    {
        return new clsWagonR();
    }

    public IAboveNormal GetAboveNormal()
    {
        return new clsSwift();
    }
}

class clsAudi : Icar
{
    public INormal GetNormal()
    {
        return new clsAudiA4();
    }

    public IAboveNormal GetAboveNormal()
    {
        return new clsAudiQ7();
    }
}
4. Next, the class which you can see below contains the method which decides whose object should be now created (you can put this method in client code instead of making new class).
C#
class clsDecider
    {
        public string GetCarDetails(int iCarType)
        {
            Icar ObjClient=null;
            switch (iCarType)
            {
                case 1:
                    ObjClient = new clsMaruti();
                    break;
                case 2:
                     ObjClient = new clsAudi();
                    break;
                default:
                    ObjClient = new clsMaruti();
                    break;
            }

            string sOutput = "Normal Car is: "+ ObjClient.GetNormal().DisplayBase()+", Above Normal car is: "+ ObjClient.GetAboveNormal().DisplayTop();
            return sOutput;
        }
    }
5. Finally, Below is my client code:
C#
clsDecider ObjDecider = new clsDecider();
string sResult= ObjDecider.GetCarDetails(2);
In client code, I have harcoded 2, you can pass this value through some control like Textbox, Radiobutton or Dropdown.

License

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


Written By
Software Developer (Senior)
India India
Linkedin profile: http://www.linkedin.com/profile/view?id=241442098

Comments and Discussions

 
GeneralMy vote of 1 Pin
Laksiriben9-Aug-16 18:32
Laksiriben9-Aug-16 18:32 
GeneralRe: My vote of 1 Pin
Vipin_Arora10-Aug-16 23:16
Vipin_Arora10-Aug-16 23:16 
QuestionThis is Factory Design Pattern not Abstract Factory Design Pattern. Pin
Arindom Ghosh6-Jan-16 2:02
professionalArindom Ghosh6-Jan-16 2:02 
AnswerRe: This is Factory Design Pattern not Abstract Factory Design Pattern. Pin
Vipin_Arora6-Jan-16 18:15
Vipin_Arora6-Jan-16 18:15 
SuggestionC# Coding Standards and Naming Conventions Pin
essixx30-Aug-15 19:34
essixx30-Aug-15 19:34 
GeneralRe: C# Coding Standards and Naming Conventions Pin
Vipin_Arora21-Jun-16 17:57
Vipin_Arora21-Jun-16 17:57 
QuestionMy Vote of 5 Pin
Malhotra Sameer2-Jan-15 4:14
professionalMalhotra Sameer2-Jan-15 4:14 
AnswerRe: My Vote of 5 Pin
Vipin_Arora3-Jan-15 2:44
Vipin_Arora3-Jan-15 2:44 
QuestionInterface instead of Abstract Class Pin
dnode25-Dec-14 3:18
dnode25-Dec-14 3:18 
GeneralRe: Interface instead of Abstract Class Pin
PIEBALDconsult25-Dec-14 3:40
mvePIEBALDconsult25-Dec-14 3:40 
QuestionNaming convention Pin
sam683ir31-Mar-14 2:51
sam683ir31-Mar-14 2:51 
AnswerRe: Naming convention Pin
Vipin_Arora31-Mar-14 4:35
Vipin_Arora31-Mar-14 4:35 
GeneralRe: Naming convention Pin
sam683ir10-Apr-14 23:03
sam683ir10-Apr-14 23:03 
GeneralRe: Naming convention Pin
Vipin_Arora10-Apr-14 23:10
Vipin_Arora10-Apr-14 23:10 
GeneralMy vote of 5 Pin
RogerGlez12-Mar-14 7:51
RogerGlez12-Mar-14 7:51 
GeneralRe: My vote of 5 Pin
Vipin_Arora12-Mar-14 23:02
Vipin_Arora12-Mar-14 23:02 
GeneralCongratulations this a very good article Pin
RogerGlez12-Mar-14 7:50
RogerGlez12-Mar-14 7:50 
GeneralRe: Congratulations this a very good article Pin
Vipin_Arora12-Mar-14 23:04
Vipin_Arora12-Mar-14 23:04 

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.