Click here to Skip to main content
12,359,751 members (61,765 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

39.1K views
16 bookmarked
Posted

Abstract Factory Design Pattern in C#

, 10 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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()

interface INormal
   {
        string DisplayBase();
   }

   interface IAboveNormal
   {
       string DisplayTop();
   }
2. Here are some concrete classes which are implementing the methods of above interfaces.
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.
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).
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:
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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionThis is Factory Design Pattern not Abstract Factory Design Pattern. Pin
arindom.ghosh876-Jan-16 2:02
memberarindom.ghosh876-Jan-16 2:02 
AnswerRe: This is Factory Design Pattern not Abstract Factory Design Pattern. Pin
Vipin_Arora6-Jan-16 18:15
memberVipin_Arora6-Jan-16 18:15 
SuggestionC# Coding Standards and Naming Conventions Pin
essixx30-Aug-15 19:34
memberessixx30-Aug-15 19:34 
GeneralRe: C# Coding Standards and Naming Conventions Pin
Vipin_Arora21-Jun-16 17:57
memberVipin_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
memberVipin_Arora3-Jan-15 2:44 
QuestionInterface instead of Abstract Class Pin
dnode25-Dec-14 3:18
memberdnode25-Dec-14 3:18 
GeneralRe: Interface instead of Abstract Class Pin
PIEBALDconsult25-Dec-14 3:40
protectorPIEBALDconsult25-Dec-14 3:40 
QuestionNaming convention Pin
sam683ir31-Mar-14 2:51
membersam683ir31-Mar-14 2:51 
AnswerRe: Naming convention Pin
Vipin_Arora31-Mar-14 4:35
memberVipin_Arora31-Mar-14 4:35 
GeneralRe: Naming convention Pin
sam683ir10-Apr-14 23:03
membersam683ir10-Apr-14 23:03 
GeneralRe: Naming convention Pin
Vipin_Arora10-Apr-14 23:10
memberVipin_Arora10-Apr-14 23:10 
GeneralMy vote of 5 Pin
RogerGlez12-Mar-14 7:51
memberRogerGlez12-Mar-14 7:51 
GeneralRe: My vote of 5 Pin
Vipin_Arora12-Mar-14 23:02
memberVipin_Arora12-Mar-14 23:02 
GeneralCongratulations this a very good article Pin
RogerGlez12-Mar-14 7:50
memberRogerGlez12-Mar-14 7:50 
GeneralRe: Congratulations this a very good article Pin
Vipin_Arora12-Mar-14 23:04
memberVipin_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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 10 Mar 2014
Article Copyright 2014 by Vipin_Arora
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid